Found 4381 Articles for MySQL

Find minimum score from the entire four columns of a table in MySQL

AmitDiwan
Updated on 01-Oct-2019 07:22:14

134 Views

To find a minimum score from the entire four columns, use MySQL LEAST() function. Let us first create a table −mysql> create table DemoTable(    Score1 int,    Score2 int,    Score3 int,    Score4 int ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88, 76, 45, 56); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(99, 78, 87, 34); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(34, 32, 56, 98); Query OK, 1 row affected (0.44 sec)Display all records from ... Read More

How do I select four random tables from a MySQL database having thousands of tables?

AmitDiwan
Updated on 01-Oct-2019 07:17:43

170 Views

To select four random tables, use ORDER BY RAND(). Following is the syntax −select TABLE_NAME AS anyAliasName from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ‘yourDatabaseName’; order by rand() limit yourLimitNumber;Let us implement the above syntax in order to select four random tables from a MySQL database that has thousands of tables.Here, LIMIT is used to set the number of records you want to fetch. Since we want 4 records, therefore we would be using LIMIT 4. Following is the query −mysql> select TABLE_NAME AS Random4TableName from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'web' order by ... Read More

Get the maximum value of a column with MySQL Aggregate function

AmitDiwan
Updated on 01-Oct-2019 07:16:29

212 Views

To get the maximum value of a column, MySQL has a predefined aggregate function MAX(). Let us first create a table −mysql> create table DemoTable (    Id int ); Query OK, 0 rows affected (0.96 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(988); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(1000); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(99); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(999); Query OK, ... Read More

Is there a way to create a MySQL “alias” while creating a VIEW?

AmitDiwan
Updated on 01-Oct-2019 07:12:36

250 Views

Yes, use the AS keyword to create a MySQL alias. Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100) ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | FirstName | +-----------+ | Chris ... Read More

MySQL ORDER BY with CASE WHEN

AmitDiwan
Updated on 01-Oct-2019 07:11:21

4K+ Views

For this, you can use the ORDER BY CASE statement. Let us first create a table −mysql> create table DemoTable order by with vas Color varchar(100) ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Red'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Green'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Blue'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Yellow'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select ... Read More

MySQL query to select column values ending with certain character/number?

AmitDiwan
Updated on 01-Oct-2019 07:06:40

2K+ Views

Let us first create a table −mysql> create table DemoTable (    Number int ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(189); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(178); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(876); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(784); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(988); Query OK, 1 row affected (0.23 sec)Display all records from the table using select statement −mysql> select ... Read More

MySQL query to update a specific cell to be empty

AmitDiwan
Updated on 01-Oct-2019 07:01:19

442 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(50) ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1001, 'Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1002, 'Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1003, 'Tom'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------+--------+ | Id | Name | ... Read More

MySQL query to sum 3 different values in a column displaying total of each value in result set?

AmitDiwan
Updated on 01-Oct-2019 06:59:33

205 Views

For this, you can use a CASE statement. Let us first create a table −mysql> create table DemoTable (    ProductName varchar(100),    ProductRating ENUM('1', '2', '3') ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Product-1', 3); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-2', 1); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Product-3', 2); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-1', 2); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Product-3', ... Read More

Inserting multiple parameter values into a single column with MySQL?

AmitDiwan
Updated on 01-Oct-2019 06:56:35

459 Views

To insert multiple parameter values into a single column, use the CONCAT_WS() or CONCAT(). Let us first create a table −mysql> create table DemoTable (    Name varchar(100),    Age int,    CountryName varchar(100),    PersonInformation text ); Query OK, 0 rows affected (0.67 sec)Following is the query to insert multiple parameter values into a single column. We will do this using the same INSERT command, which is used to insert records in a MySQL table −mysql> insert into DemoTable values('John', 21, 'US', concat_ws('-', Name, Age, CountryName)); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris', 22, ... Read More

Select into a user-defined variable with MySQL

AmitDiwan
Updated on 01-Oct-2019 06:54:37

439 Views

For user-defined variables, we use @ in MySQL. Following is the syntax. Here, @anyVariableName is our user-defined variable −select yourColumnName into @anyVariableName from yourTableName where yourCondition;Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100) ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.10 sec)Display all ... Read More

Advertisements