Found 4378 Articles for MySQL

MySQL automatic string to integer casting in WHERE clause to fetch a specific id

AmitDiwan
Updated on 11-Nov-2019 10:24:11

300 Views

If the string begins with integer then it converts the string to integer, otherwise it won’t. Let us first create a −mysql> create table DemoTable1390    -> (    -> StudentId varchar(20)    -> ); Query OK, 0 rows affected (0.93 sec)Insert some records in the table using insert −mysql> insert into DemoTable1390 values('563_John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1390 values('1001_Carol_Taylor'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1390 values('David_Miller_789'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable1390 values('456_AdamSmith'); Query OK, 1 row affected (0.11 sec)Display all records from ... Read More

Make all column names lower case in MySQL with a single query

AmitDiwan
Updated on 11-Nov-2019 10:20:59

1K+ Views

Let us first create a −mysql> create table DemoTable1    -> (    -> StudentFirstName varchar(20),    -> StudentLastName varchar(20),    -> StudentAge int,    -> StudentCountryName varchar(20)    -> ); Query OK, 0 rows affected (4.20 sec)Let us now make all column names lower case in MySQL −mysql> select concat('alter table ', table_name, ' change `', column_name, '` `',    -> lower(column_name), '` ', column_type, ';')    -> from information_schema.columns  where table_schema = 'demo';This will produce the following output −+-----------------------------------------------------------------------------------------------------------------+ | concat('alter table ', table_name, ' change `', column_name, '` `', lower(column_name), '` ', column_type, ';') | +-----------------------------------------------------------------------------------------------------------------+ | ... Read More

Find exact string value with COLLATE in MySQL?

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

108 Views

Let us first create a −mysql> create table DemoTable1620    -> (    -> Subject varchar(20)    -> ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert −mysql> insert into DemoTable1620 values('mysql'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1620 values('MySql'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1620 values('mYSQL'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1620 values('MySQL'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1620 values('MYSQL'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select ... Read More

How to select multiple max values which would be duplicate values as well in MYSQL?

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

2K+ Views

For this, use the join concept. Let us first create a −mysql> create table DemoTable1389    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentMarks int    -> ); Query OK, 0 rows affected (2.73 sec)Insert some records in the table using insert command. Here, we have inserted duplicate values as well −mysql> insert into DemoTable1389(StudentMarks) values(40); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1389(StudentMarks) values(40); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1389(StudentMarks) values(68); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1389(StudentMarks) values(78); Query OK, ... Read More

Display the contents of a VIEW in MySQL?

AmitDiwan
Updated on 11-Nov-2019 10:15:27

1K+ Views

Following is the syntax −select * from yourViewName;Let us first create a table −mysql> create table DemoTable1388    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(40)    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1388(StudentName) values('Chris'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1388(StudentName) values('Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1388(StudentName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1388(StudentName) values('Mike'); Query OK, 1 row affected (0.29 sec)Display all ... Read More

How to combine duplicate values into one with corresponding value separated by hyphens in MySQL?

AmitDiwan
Updated on 08-Jul-2020 12:36:56

4K+ Views

To combine, use GROUP_CONCAT() function to combine some attributes in two rows into one. As a separator, use hyphens.Let us first create a table −mysql> create table DemoTable1386    -> (    -> Title varchar(255),    -> Name varchar(60)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1386 values('Introduction to MySQL', 'Paul DuBois'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1386 values('Java in Depth', 'Khalid Mughal'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable1386 values('Introduction to MySQL', 'Russell Dyer'); Query OK, ... Read More

How to obtain multiple rows in a single MySQL query?

AmitDiwan
Updated on 11-Nov-2019 10:11:54

161 Views

To obtain multiple rows in a single MySQL query, use LIKE operator. Let us first create a table −mysql> create table DemoTable1385    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1385(Name) values('Chris Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1385(Name) values('Adam Smith'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1385(Name) values('Carol Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1385(Name) values('John ... Read More

Insert multiple rows in a single MySQL query

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

387 Views

Let us first create a table −mysql> create table DemoTable1384    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAge int    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command. Here, we are inserting multiple rows in a single query −mysql> insert into DemoTable1384(StudentName, StudentAge) values('Chris Brown', 21), ('David Miller', 22), -> ('Carol Taylor', 19), ('Adam Smith', 23); Query OK, 4 rows affected (0.11 sec) Records: 4  Duplicates: 0  Warnings: 0Display all records from the table using select statement −mysql> select * ... Read More

MySQL query to find the top two highest scores

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

453 Views

For this, use aggregate function MAX(). Let us first create a table −mysql> create table DemoTable1383    -> (    -> Id int,    -> PlayerScore int    -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1383 values(200, 78); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1383 values(200, 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1383 values(200, 89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1383 values(200, 87); Query OK, 1 row affected (0.29 sec) mysql> insert into ... Read More

How to update a MySQL table by swapping two column values?

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

344 Views

To swap two values in a column, use CASE WHEN statement. Let us first create a table −mysql> create table DemoTable1382    -> (    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1382 values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1382 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1382 values('Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1382 values('Bob'); Query OK, 1 row affected (0.17 sec)Display all records from the table using ... Read More

Advertisements