Found 4378 Articles for MySQL

Is there any way to use values from a JSON object in a MySQL Select statement?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

155 Views

Yes, you can use json_extract(). Let us first create a table −mysql> create table DemoTable    -> (    -> Data json    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('{"Name": "John", "CountryName": "US"}'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable values('{"Name": "Chris", "CountryName": "UK"}'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+----------------------------------------+ | Data                               ... Read More

MySQL query to get the highest value from a single row with multiple columns

Rama Giri
Updated on 30-Jul-2019 22:30:26

145 Views

To get the highest value, use the GREATEST() method. Let us first create a table −mysql> create table DemoTable    -> (    -> Value1 int,    -> Value2 int,    -> Value3 int    -> ); Query OK, 0 rows affected (1.29 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 600, 400); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 100 | 600 | 400    | ... Read More

MySQL select any one field out of two with respect to the value of a third field?

Rama Giri
Updated on 30-Jul-2019 22:30:26

47 Views

For this, use IF(). Let us first create a table −mysql> create table DemoTable    -> (    -> PlayerName varchar(100),    -> PlayerScore int,    -> PlayerStatus varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 88, 'BAD'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris', 78, 'BAD'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Robert', 90, 'BAD'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David', 80, 'BAD'); Query ... Read More

MySQL query to get first two highest column values from a table?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

397 Views

To get the first two highest columns, use ORDER BY. With that, use LIMIT 2 to get only the first 2 −select *from yourTableName order by yourColumnName DESC LIMIT 2;Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected ... Read More

Finding a specific row which has several ids separated by comma in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

92 Views

To find a row, use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable    -> (    -> ListOfIds varchar(200)    -> ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('100, 2093, 678, 686'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('0595, 9585, 4885, 95959'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('0059954, 95986884, 9059596, 9005'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------------------------------+ ... Read More

Display different variables in MySQL using LIKE?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

88 Views

Following is the syntax −show variables where Variable_name like 'yourVariable1%' or Variable_name like 'yourVariable2%', .............N;Let us implement the above syntax to show(more than one) variables −mysql> show variables where Variable_name like 'key%' or Variable_name like 'innodb_undo%' or Variable_name like 'innodb_log%';Output+------------------------------------+----------+ | Variable_name                      | Value | +------------------------------------+----------+ | innodb_log_buffer_size             | 1048576 | | innodb_log_checksums               | ON | | innodb_log_compressed_pages        | ON ... Read More

How to update a timestamp field of a MySQL table?

Rama Giri
Updated on 30-Jul-2019 22:30:26

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> PunchOut timestamp,    -> PunchStatus tinyint(1)    -> ); Query OK,  0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-31 6:30:10', 1); Query OK,  1 row affected (0.22 sec) mysql> insert into DemoTable values('2019-02-06 4:10:13', 0); Query OK,  1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-12-16 03:00:30', 0); Query OK,  1 row affected (0.16 sec) mysql> insert into DemoTable values('2016-11-25 02:10:00', 1); Query OK,  1 row affected (0.22 sec)Display all records from the table using select statemen −mysql> select *from DemoTable;Output+---------------------+-------------+ | PunchOut            | PunchStatus | +---------------------+-------------+ | 2019-01-31 06:30:10 |           1 | | 2019-02-06 04:10:13 |           0 | | 2018-12-16 03:00:30 | ... Read More

MySQL query to return only the rows which are numeric?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

771 Views

Use REGEXP to return only the numeric rows. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John74747'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('8494575Carol'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('985755645'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Carol-9032'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('101'); ... Read More

What is the most efficient way to select a specific number of random rows in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

431 Views

Use RAND() method for random and to limit a number of records, use the LIMIT() method in MySQL.Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(300); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(600); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(700); Query OK, 1 row ... Read More

How can I find the percentage of my users whose birth date is between 1980 and 1996 in MySQL?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

97 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> DateOfBirth varchar(100)    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019/01/31'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('1980/02/01'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('1985/04/10'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('1995/06/04'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('1990/12/24'); Query OK, 1 row affected (0.18 sec) ... Read More

Advertisements