Found 4378 Articles for MySQL

Reshuffle the values in a table with MySQL

AmitDiwan
Updated on 09-Sep-2019 08:44:50

83 Views

To reshuffle the values in a table, use MySQL RAND().Let us first create a table −mysql> create table DemoTable792 ( Name varchar(100), Subject varchar(100) ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable792 values('Chris', 'MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable792 values('David', 'MySQL'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable792 values('Robert', 'MongoDB'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable792 values('Carol', 'MongoDB'); Query OK, 1 row affected (0.18 sec)Display all records ... Read More

MySQL query to exclude some of the values from the table

AmitDiwan
Updated on 09-Sep-2019 08:43:11

1K+ Views

Use NOT IN() to exclude some of the values from the table.Let us first create a table −mysql> create table DemoTable791 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100) ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable791(FirstName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable791(FirstName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable791(FirstName) values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable791(FirstName) values('Mike'); Query OK, 1 row affected (0.11 ... Read More

Get the highest score value from a single column and the greatest from two columns in MySQL

AmitDiwan
Updated on 09-Sep-2019 08:41:28

101 Views

Let us first create a table −mysql> create table DemoTable790 ( Score1 int, Score2 int ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command −mysql> insert into DemoTable790 values(98, 76); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable790 values(78, 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable790 values(85, 68); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable790;This will produce the following output -+--------+--------+ | Score1 | Score2 | +--------+--------+ ... Read More

Display records where first and last name begins with the same letter in MySQL

AmitDiwan
Updated on 09-Sep-2019 08:38:59

645 Views

To check for the 1st letter of the first and last name, you need to use the LEFT().Let us first create a table −mysql> create table DemoTable789 ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable789 values('Adam', 'Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable789 values('Tom', 'Taylor'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable789 values('Bob', 'Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable789 values('David', 'Miller'); Query ... Read More

Sort data for duplicate record in MySQL

AmitDiwan
Updated on 09-Sep-2019 08:37:00

416 Views

Use ORDER BY to sort data for duplicate record.Let us first create a table −mysql> create table DemoTable788 ( FirstName varchar(100), Score int ); Query OK, 0 rows affected (1.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable788 values('Chris', 78); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable788 values('Robert', 67); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable788 values('Chris', 98); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable788 values('Chris', 56); Query OK, 1 row affected (0.15 sec) mysql> insert into ... Read More

Select a specific value between two column values in MySQL?

AmitDiwan
Updated on 09-Sep-2019 08:35:21

2K+ Views

Let us first create a table −mysql> create table DemoTable787 ( Score1 int, Score2 int, Name varchar(100) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable787 values(34, 56, 'Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable787 values(73, 86, 'Robert'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable787 values(90, 99, 'David'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable787 values(80, 89, 'Adam'); Query OK, 1 row affected (0.21 sec)Display all records ... Read More

How to display column values as CSV in MySQL?

AmitDiwan
Updated on 09-Sep-2019 08:33:31

510 Views

To display column values as CSV, use GROUP_CONCAT().Let us first create a table −mysql> create table DemoTable786 (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100) )   AUTO_INCREMENT=101; Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable786(StudentName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable786(StudentName) values('Robert'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable786(StudentName) values('Mike'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable786(StudentName) values('Sam'); Query OK, 1 row affected (0.12 sec)Display all records from the table ... Read More

How can I select rows which fall on a specific day of week in MySQL?

AmitDiwan
Updated on 09-Sep-2019 08:31:15

127 Views

For specific day of week, use DAYOFWEEK().Let us first create a table −mysql> create table DemoTable785 ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(100), ShoppingDate date ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable785(CustomerName, ShoppingDate) values('Chris', '2019-07-03'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable785(CustomerName, ShoppingDate) values('Robert', '2019-07-01'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable785(CustomerName, ShoppingDate) values('David', '2019-07-06'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable785(CustomerName, ShoppingDate) ... Read More

MySQL query to sort column values and ignoring quotes on one of the values

AmitDiwan
Updated on 09-Sep-2019 08:16:48

149 Views

To ignore quotes while ordering column values, use ORDER BY TRIM().Let us first create a table −mysql> create table DemoTable784 ( Message varchar(100) ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable784 values('Good'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable784 values('\"This is not a Message\"'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable784 values('Bye'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable784 values('Hello'); Query OK, 1 row affected (0.15 sec)Display all records from the table using ... Read More

Count and sort rows with a single MySQL query

AmitDiwan
Updated on 09-Sep-2019 08:14:30

136 Views

Let us first create a table −mysql> create table DemoTable783 ( FirstName varchar(100) ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable783 values('Adam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable783 values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable783 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable783 values('Adam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable783 values('Adam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable783 values('Robert'); Query OK, 1 ... Read More

Advertisements