Found 4378 Articles for MySQL

Concatenate two values from the same column with different conditions in MySQL

AmitDiwan
Updated on 26-Dec-2019 07:35:39

902 Views

For this, you can use group_concat() with aggregate function. Let us first create a table −mysql> create table DemoTable1869      (      Id int,      Subject varchar(20 ),      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1869 values(100, 'MySQL', 'John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1869 values(100, 'MongoDB', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1869 values(101, 'MySQL', 'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1869 values(101, ... Read More

How to derive value of a field from another field in MySQL?

AmitDiwan
Updated on 26-Dec-2019 07:31:05

317 Views

For this, you can use the concept of user defined variable. Let us first create a table −mysql> create table DemoTable1868      (      Value int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1868 values(10); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(20); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(30); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(40); Query OK, 1 row affected (0.00 sec)Display all records from the table using ... Read More

How to list all variables initialized by SET operator in MySQL?

AmitDiwan
Updated on 26-Dec-2019 07:22:55

237 Views

To list all variables initialized by SET operator, the syntax is as follows −select * from performance_schema.user_variables_by_thread;Here is the query to set the variable −mysql> set @FirstName='John'; Query OK, 0 rows affected (0.00 sec) mysql> set @LastName='Doe'; Query OK, 0 rows affected (0.00 sec)Here is the query to display the list of all variables initialized by SET operator. This list includes the variables set above −mysql> select * from performance_schema.user_variables_by_thread;This will produce the following output −+-----------+---------------+----------------+ | THREAD_ID | VARIABLE_NAME | VARIABLE_VALUE | +-----------+---------------+----------------+ |       120 | TotalAmount   |           5000 | ... Read More

Increment date/time value by second with MySQL query?

AmitDiwan
Updated on 26-Dec-2019 07:21:06

459 Views

For this, use date_add() with interval command. Let us first create a table −mysql> create table DemoTable1867      (      ArrivalTime datetime      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1867 values('2019-10-12 12:34:45'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1867 values('2019-10-12 10:04:15'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1867 values('2019-10-12 11:00:23'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1867; This will produce the following output ... Read More

MySQL query to get string from one column and find its position in another column with comma separated values?

AmitDiwan
Updated on 26-Dec-2019 07:19:48

257 Views

For this, use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable1866      (      Value1 int,      ListOfValues varchar(100)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1866 values(56, '78, 56, 98, 95'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1866 values(103, '103, 90, 102, 104'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1866 values(77, '34, 45, 77, 78'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> ... Read More

Trying to round a calculation to two decimal places in a new column with MySQL?

AmitDiwan
Updated on 26-Dec-2019 07:18:00

434 Views

To round, use MySQL ROUND() function. Let us first create a table −mysql> create table DemoTable1865      (      Value1 int,      Value2 int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1865 values(40, 60); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1865 values(100, 400); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1865 values(545, 896); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1865;This will produce the ... Read More

How to assign the result of a MySQL query into a variable?

AmitDiwan
Updated on 26-Dec-2019 07:16:28

849 Views

Use @anyVariableName to assign the result of a query into a variable. Let us first create a table −mysql> create table DemoTable1864      (      Id int,      FirstName varchar(20),      LastName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1864 values(101, 'Chris', 'Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1864 values(102, 'David', 'Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1864 values(103, 'Adam', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

How to set default value for empty row in MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:54:31

550 Views

To set default value for empty row, use the concept of COALESCE(). Let us first create a table −mysql> create table DemoTable1863      (      FirstName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1863 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values(NULL); Query OK, 1 row affected (0.00 sec)Display all records from the table ... Read More

How to find records with a null value in a set of columns with MySQL

AmitDiwan
Updated on 26-Dec-2019 06:51:44

149 Views

For this, use the concept of GREATEST(). Let us first create a table −mysql> create table DemoTable1862      (      Value1 int,      Value2 int,      Value3 int,      Value4 int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1862 values(43, 34, 56, 42); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1862 values(NULL, 78, 65, NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1862 values(110, NULL, 78, NULL); Query OK, 1 row affected (0.00 sec)Display all ... Read More

How to find rows with exact value in one or more columns with MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:48:18

350 Views

For this, you can use GROUP BY HAVING with subquery. Let us first create a table −mysql> create table DemoTable1861      (      Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,      Name varchar(20),      Marks int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1861(Name, Marks) values('John', 45); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1861(Name, Marks) values('Chris', 74); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1861(Name, Marks) values('David', 89); Query OK, 1 row affected (0.00 sec) ... Read More

Advertisements