Found 4378 Articles for MySQL

Implement CASE statement with WHEN clause to check for the existence of a record in MySQL

AmitDiwan
Updated on 26-Sep-2019 08:14:59

140 Views

Let us first create a table −mysql> create table DemoTable (    UserId int,    UserName varchar(100) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(101, 'Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(103, 'Mike'); 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 ... Read More

How can I achieve similar result like using a loop in MySQL WHERE clause to display only alternate ID records?

AmitDiwan
Updated on 26-Sep-2019 08:13:29

100 Views

Get similar results using MySQL IN(). Let us first create a table −mysql> create table DemoTable (    ClientId int,    ClientName varchar(100),    ClientAge int ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris', 34); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(101, 'Robert', 31); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(103, 'David', 33); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(104, 'Mike', 45); Query OK, 1 row affected (0.17 sec) mysql> insert ... Read More

MySQL query to order by current day and month?

AmitDiwan
Updated on 26-Sep-2019 08:10:26

234 Views

For this, you can use the ORDER BY CASE statement. Let us first create a table −mysql> create table DemoTable (    DueDate date ); Query OK, 0 rows affected (0.56 sec)Note − Let’s say the current date is 2019-07-22.Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-03-10'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-08-25'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-12-31'); Query OK, 1 row affected (0.17 sec)Display all records from the ... Read More

Fix a row value and then ORDER BY DESC rest of the values in MySQL

AmitDiwan
Updated on 26-Sep-2019 08:08:56

76 Views

Let us first create a table −mysql> create table DemoTable (    id int ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values(7); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(6); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(11); Query OK, 1 row affected ... Read More

How to remove unconvertable characters to ASCII with SELECT in MySQL?

AmitDiwan
Updated on 26-Sep-2019 08:06:59

1K+ Views

Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('€986'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('§97'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+ | Value | +--------+ | €986 | | §97 +--------+ 2 rows in set (0.00 sec)Following is the query to remove unconvertable characters to ASCII ... Read More

Select distinct values from two columns in MySQL?

AmitDiwan
Updated on 26-Sep-2019 08:04:17

450 Views

To select distinct values from two columns, use UNION. Let us first create a table −mysql> create table DemoTable1 (    Value1 int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(1); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(2); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1 values(3); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(4); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will ... Read More

Write a single MySQL query to return the ID from the corresponding row which is a NOT NULL value

AmitDiwan
Updated on 26-Sep-2019 08:00:29

77 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentName varchar(100) ); Query OK, 0 rows affected (0.85 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(NULL, 'Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL, 'Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(101, 'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(102, 'Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(103, 'Sam'); Query OK, 1 row affected (0.18 sec)Display all records ... Read More

Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?

AmitDiwan
Updated on 26-Sep-2019 07:51:01

442 Views

Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('190'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('230'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('120'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('189'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | +-------+ ... Read More

Perform MySQL SELECT on dates inserted into the table as VARCHAR values

AmitDiwan
Updated on 26-Sep-2019 07:48:46

52 Views

Let us first create a table −mysql> create table DemoTable (    DueDate varchar(100) ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('21/10/2018'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('18/08/2019'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('01/12/2012'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('31/01/2016'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------+ | DueDate ... Read More

Copy column values from one table into another matching IDs in MySQL

AmitDiwan
Updated on 26-Sep-2019 07:46:51

891 Views

Let us first create a table −mysql> create table DemoTable1 (    PersonId int,    Value int ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(100, 78); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable1 values(101, 67); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values(102, 89); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+----------+-------+ | PersonId | Value | +----------+-------+ |      100 ... Read More

Advertisements