Found 4218 Articles for MySQLi

MySQL format time with lowercase am/pm?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

750 Views

To format MySQL time with lowercase am/pm, use the LOWER() as well as DATE_FORMAT().Let us first create a table −mysql> create table formatTime    -> (    -> LoginTime time    -> ); Query OK, 0 rows affected (0.56 sec)Following is the query to insert records in the table using insert command −mysql> insert into formatTime values('12:40:34'); Query OK, 1 row affected (0.20 sec) mysql> insert into formatTime values('14:10:58'); Query OK, 1 row affected (0.13 sec) mysql> insert into formatTime values('16:56:40'); Query OK, 1 row affected (0.18 sec) mysql> insert into formatTime values('10:12:14'); Query OK, 1 row ... Read More

Add10 minutes to MySQL datetime format?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

393 Views

Use DATE_ADD() to add 10 minutes to datetime format. Following is the syntax −select date_add(yourColumnName ,interval 10 minute) from yourTableName;Let us first create a table −mysql> create table add10MinuteDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> DelayDatetime datetime    -> ); Query OK, 0 rows affected (0.83 sec)Following is the query to insert records in the table using insert command −mysql> insert into add10MinuteDemo(DelayDatetime) values('2019-01-23 12:45:56'); Query OK, 1 row affected (0.16 sec) mysql> insert into add10MinuteDemo(DelayDatetime) values('2019-03-25 10:30:23'); Query OK, 1 row affected (0.19 sec) mysql> insert into add10MinuteDemo(DelayDatetime) values('2019-04-21 04:04:30'); Query ... Read More

Can we GROUP BY one column and select all data in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

1K+ Views

Yes, you can use group_concat() for this. Let us first create a table −mysql> create table groupByOneSelectAll    -> (    -> StudentDetails varchar(100),    -> StudentName varchar(100)    -> ); Query OK, 0 rows affected (0.91 sec)Following is the query to insert some records in the table using insert command −mysql> insert into groupByOneSelectAll values('StudentFirstName', 'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName', 'Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName', 'Robert'); Query OK, 1 row affected (0.65 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName', 'Bob'); Query ... Read More

How to use the CAST function in a MySQL SELECT statement?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

376 Views

The CAST() function in MySQL converts a value of any type into a value that has a specified type. Let us first create a table −mysql> create table castFunctionDemo    -> (    -> ShippingDate date    -> ); Query OK, 0 rows affected (0.74 sec)Following is the query to insert some records in the table using insert command −mysql> insert into castFunctionDemo values('2019-01-31'); Query OK, 1 row affected (0.20 sec) mysql> insert into castFunctionDemo values('2018-07-12'); Query OK, 1 row affected (0.16 sec) mysql> insert into castFunctionDemo values('2016-12-06'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

How to compare timestamps in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

4K+ Views

To compare timestamps in MySQL, you can use DATE(). Let us first create a table−mysql> create table comparingTimestampDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> AdmissionDate timestamp    -> ); Query OK, 0 rows affected (0.54 sec)Following is the query to insert records in the table using insert command −mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-03-31'); Query OK, 1 row affected (0.13 sec) mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-15'); Query OK, 1 row affected (0.17 sec) mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-03-29'); Query OK, 1 ... Read More

MySQL select only duplicate records from database and display the count as well?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

405 Views

To select only duplicate records from database and display the count, use HAVING along with aggregate function count(). Let us first create a table −mysql> create table duplicateRecords    -> (    -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientName varchar(20)    -> ); Query OK, 0 rows affected (0.49 sec)Following is the query to insert records in the table using insert command −mysql> insert into duplicateRecords(ClientName) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into duplicateRecords(ClientName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into duplicateRecords(ClientName) values('John'); Query OK, 1 row affected ... Read More

How to calculate value from multiple columns in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

1K+ Views

To calculate a value from multiple columns, use GROUP BY. Following is the syntax −select yourColumnName1, sum(yourColumnName2*yourColumnName3) AS anyAliasName from yourTableName group by yourColumnName1;Let us first create a table −mysql> create table calculateValueDemo    -> (    -> Id int,    -> ProductPrice int,    -> ProductWeight int    -> ); Query OK, 0 rows affected (0.56 sec)Following is the query to insert records in the table using insert command −mysql> insert into calculateValueDemo values(100, 35, 5); Query OK, 1 row affected (0.16 sec) mysql> insert into calculateValueDemo values(101, 50, 3); Query OK, 1 row affected (0.16 sec) ... Read More

How to select distinct value from one MySQL column only?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

To select distinct value from one column only, you can use aggregate function MAX() along with GROUP BY. Let us first create a table −mysql> create table distinctFromOneColumn    -> (    -> StudentId int,    -> StudentName varchar(100)    -> ); Query OK, 0 rows affected (0.77 sec)Following is the query to insert records in the table using insert command −mysql> insert into distinctFromOneColumn values(1001, 'John'); Query OK, 1 row affected (0.15 sec) mysql> insert into distinctFromOneColumn values(1002, 'Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into distinctFromOneColumn values(1001, 'Sam'); Query OK, 1 row affected ... Read More

Resolve MySQL ERROR 1064 (42000): You have an error in your syntax?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

8K+ Views

This error occurs if let’s say you used var_char instead of varchar type. To remove this type of error, use, for example, varchar(100) instead of var_char(100).Let us now see how this error occurs −mysql> create table removeErrorDemo    -> (    -> StudentId int,    -> StudentName var_char(50)    -> );The following is the output displaying the error −ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'var_char(50) )' at line 4Now let us remove the error. Here is the query ... Read More

MySQL search if more than one string contains special characters?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

3K+ Views

To search if strings contain special characters, you can use REGEXP. Following is the syntax −select * from yourTableName where yourColumnName REGEXP '[^a-zA-Z0-9]';Let us first create a table −mysql> create table specialCharactersDemo    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert records in the table using insert command. Following is the query −mysql> insert into specialCharactersDemo values('STU_1234'); Query OK, 1 row affected (0.15 sec) mysql> insert into specialCharactersDemo values('STU567'); Query OK, 1 row affected (0.14 sec) mysql> insert into specialCharactersDemo values('STU#1234'); Query OK, 1 row affected (0.13 sec) mysql> insert into specialCharactersDemo ... Read More

Advertisements