Found 4378 Articles for MySQL

How to compare timestamps in MySQL?

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

3K+ 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

322 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

6K+ 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

2K+ 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

MySQL query to display all the fields that contain a capital letter?

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

693 Views

To display all the fields that contain a capital letter, use the RLIKE that performs a pattern match of a string expression against a pattern.Let us first create a table −mysql> create table contains_capital_letterDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (1.42 sec)Following is the query to insert some records in the table using insert command −mysql> insert into contains_capital_letterDemo(Name) values('Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into contains_capital_letterDemo(Name) values('larry'); Query OK, 1 row affected (0.12 sec) mysql> ... Read More

Update multiple rows in a single column in MySQL?

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

3K+ Views

To update multiple rows in a single column, use CASE statement. Let us first create a table −mysql> create table updateMultipleRowsDemo    -> (    -> StudentId int,    -> StudentMathScore int    -> ); Query OK, 0 rows affected (0.63 sec)Following is the query to insert records in the table using insert command −mysql> insert into updateMultipleRowsDemo values(10001, 67); Query OK, 1 row affected (0.14 sec) mysql> insert into updateMultipleRowsDemo values(10002, 69); Query OK, 1 row affected (0.15 sec) mysql> insert into updateMultipleRowsDemo values(10003, 89); Query OK, 1 row affected (0.14 sec) mysql> insert into updateMultipleRowsDemo values(10004, 99); Query ... Read More

Convert UNIX timestamp into human readable format in MySQL?

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

723 Views

To convert UNIX timestamp into a human-readable format, use the FROM_UNIXTIME() method.Let us first create a table −mysql> create table timeConversionDemo    -> (    -> dateTimeConversion bigint    -> ); Query OK, 0 rows affected (0.45 sec)Following is the query to insert records in the table using insert command −mysql> insert into timeConversionDemo values(1554316200); Query OK, 1 row affected (0.14 sec) mysql> insert into timeConversionDemo values(1546194600); Query OK, 1 row affected (0.22 sec) mysql> insert into timeConversionDemo values(1511548200 ); Query OK, 1 row affected (0.21 sec)Following is the query to display all records from the table using ... Read More

How to sort more than one column at a time in MySQL?

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

87 Views

To sort more than one column at a time, you can use ORDER BY clause. Following is the syntax −select yourColumnName1, yourColumnName2, yourColumnName3 from yourTableName order by yourColumnName2, yourColumnName3;Let us first create a table −mysql> create table doubleSortDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100),    -> StudentCountryName varchar(10)    -> ); Query OK, 0 rows affected (0.71 sec)Following is the query to insert records in the table using insert command −mysql> insert into doubleSortDemo(StudentName, StudentCountryName) values('John', 'AUS'); Query OK, 1 row affected (0.21 sec) mysql> insert into doubleSortDemo(StudentName, StudentCountryName) values('Sam', ... Read More

Advertisements