Using the Entire Expression in MySQL WHERE Clause

AmitDiwan
Updated on 11-Nov-2019 11:02:48

152 Views

Let us see an example and create a −mysql> create table DemoTable1406    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert −mysql> insert into DemoTable1406 values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1406 values(50); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1406 values(40); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable1406 values(30); Query OK, 1 row affected (0.11 sec)Display all records from the table using select −mysql> select * from DemoTable1406;This will produce the following ... Read More

Update All Rows in MySQL and Remove Unnecessary Whitespaces

AmitDiwan
Updated on 11-Nov-2019 11:01:29

99 Views

To remove unnecessary whitespaces, use TRIM() in MySQL. Let us first create a −mysql> create table DemoTable1405    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert −mysql> insert into DemoTable1405 values('   Chris', ' Brown '); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1405 values('David      ', ' Miller '); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1405 values('     Carol ', ' Taylor '); Query OK, 1 row affected (0.19 sec)Display all ... Read More

Display Month Names and Year from Date Records in MySQL

AmitDiwan
Updated on 11-Nov-2019 10:57:08

488 Views

Let us first create a −mysql> create table DemoTable1619    -> (    -> ArrivalTime datetime    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert −mysql> insert into DemoTable1619 values(now()); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable1619 values(curdate()); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1619 values('2019-12-31'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select −mysql> select * from DemoTable1619;This will produce the following output −+---------------------+ | ArrivalTime         | +---------------------+ | 2019-10-20 15:02:12 | | ... Read More

Copy IP Address from Varchar to Integer in MySQL

AmitDiwan
Updated on 11-Nov-2019 10:55:05

260 Views

For this, you can use INET_ATON(). Let us first create a −mysql> create table DemoTable1404    -> (    -> IpAddress varchar(40)    -> ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert −mysql> insert into DemoTable1404 values('192.168.120.0'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable1404 values('192.168.120.20'); Query OK, 1 row affected (0.60 sec) mysql> insert into DemoTable1404 values('224.0.0.0'); Query OK, 1 row affected (0.42 sec)Display all records from the table using select −mysql> select * from DemoTable1404;This will produce the following output −+----------------+ | IpAddress      | +----------------+ | ... Read More

Update Table and Order Dates in MySQL

AmitDiwan
Updated on 11-Nov-2019 10:52:35

420 Views

You cannot use UPDATE command with ORDER BY clause, but you can use SELECT statement with ORDER BY DESC.Let us first create a −mysql> create table DemoTable1403    -> (    -> DueDate timestamp    -> ); Query OK, 0 rows affected (1.26 sec)Insert some records in the table using insert −mysql> insert into DemoTable1403 values('2019-09-29'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1403 values('2016-02-21'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1403 values('2018-01-31'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable1403 values('2017-12-01'); Query OK, 1 row affected (0.27 sec)Display all ... Read More

Display Substring in MySQL Based on String Length

AmitDiwan
Updated on 11-Nov-2019 10:50:57

182 Views

For this, you can use substring() function in MySQL. For conditions, use MySQL CASE statement. Let us first create a −mysql> create table DemoTable1402    -> (    -> EmployeeName varchar(40)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert −mysql> insert into DemoTable1402 values('Adam Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1402 values('Chris Brown'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1402 values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1402 values('Carol Taylor'); Query OK, 1 row affected (0.10 sec)Display ... Read More

Backticks in MySQL Update Query Set Part

AmitDiwan
Updated on 11-Nov-2019 10:48:35

193 Views

Backticks would work if written correctly as in the below syntax −update `yourTableName` set `yourTableName`.`yourColumnName`='yourNewValue' where yourCondition;Let us first create a −mysql> create table `DemoTable_1401`    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert −mysql> insert into `DemoTable_1401` values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into `DemoTable_1401` values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into `DemoTable_1401` values('Bob'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select −mysql> select * from `DemoTable_1401`;This will produce the ... Read More

Implement Candidate Key in MySQL Table

AmitDiwan
Updated on 11-Nov-2019 10:47:16

3K+ Views

Each relation may have one or more candidate key. One of these candidate keys is called Primary Key. Each candidate key qualifies for Primary Key. Therefore, candidates for Primary Key is called Candidate Key. To implement candidate key in MySQL, set more than one column as unique key. These keys would qualify for candidate key as in the below syntax −alter table yourTableName add unique key anyName(yourColumnName1, yourColumnName2);Let us first create a −mysql> create table DemoTable1400    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(40),    -> Age int    -> ); Query ... Read More

Get Next Primary Key Without Adding New Record in MySQL

AmitDiwan
Updated on 11-Nov-2019 10:45:43

90 Views

No, it is possible to get the next primary key without adding a new record. Let us first create a −mysql> create table DemoTable1399    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert −mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.07 sec)Display all records from the table using select −mysql> select ... Read More

Find Percentage from Marks in MySQL

AmitDiwan
Updated on 11-Nov-2019 10:42:56

571 Views

Let us first create a −mysql> create table DemoTable1398    -> (    -> Marks int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert −mysql> insert into DemoTable1398 values(78); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1398 values(82); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1398 values(90); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1398 values(98); Query OK, 1 row affected (0.12 sec)Display all records from the table using select −mysql> select * from DemoTable1398;This will produce the following output −+-------+ | ... Read More

Advertisements