Found 4381 Articles for MySQL

How to compare the first date and the last date from a MySQL column with date records?

AmitDiwan
Updated on 30-Sep-2019 07:49:45

343 Views

To compare the first date with the last date, use TIME_TO_SEC() along with MAX() and MIN(). Let us first create a table −mysql> create table DemoTable (    UserName varchar(100),    UserPostDatetime datetime ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', '2019-08-24 11:10:00'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam', '2019-08-24 11:20:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Adam', '2019-08-24 11:50:00'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> ... Read More

How to get multiple rows in a single MySQL query?

AmitDiwan
Updated on 30-Sep-2019 07:48:08

772 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(110, 'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(120, 'Robert'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(130, 'Mike'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

Can we use WHERE clause inside MySQL CASE statement?

AmitDiwan
Updated on 30-Sep-2019 07:46:16

298 Views

For this, use the CASE WHEN statement. Let us first create a table −mysql> create table DemoTable1040 (    Value1 int,    Value2 int,    Value3 int ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1040 values(10, 30, 1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1040 values(40, 20, 50); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1040 values(80, 90, 100); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable1040;This will produce the ... Read More

How to get left substring in MySQL from a column with file path? Display the entire file path string excluding the file name?

AmitDiwan
Updated on 30-Sep-2019 07:39:52

375 Views

To get the left substring, use LEFT() along with substring_index(). For example, let’s say the file path is −“/MyFile/JavaProgram/Hello.java “Here, we will see how to display the entire file path except for the file name i.e. −/MyFile/JavaProgram/Let us first create a table −mysql> create table DemoTable (    FileLocation text ); Query OK, 0 rows affected (0.57 secInsert some records in the table using insert command −mysql> insert into DemoTable values('/MyFile/JavaProgram/Hello.java'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('/C/AllPrograms/animation.gif'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('/E/FavFile/ChatProgram.java'); Query OK, 1 row affected ... Read More

How to get the fourth highest value using MySQL query?

AmitDiwan
Updated on 30-Sep-2019 07:30:53

330 Views

To get the fourth-highest value, use LIMIT OFFSET along with ORDER BY. Let us first create a table −mysql> create table DemoTable (    Amount int ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(980); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(670); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(890); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(995); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(198); Query OK, 1 row affected ... Read More

Add a column count in a MySQL query on the basis of last name records?

AmitDiwan
Updated on 30-Sep-2019 07:29:38

436 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (1.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName) values('David', 'Miller'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable(FirstName, LastName) values('Carol', 'Miller'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable(FirstName, LastName) values('John', 'Doe'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

How to suppress warnings in MySQL?

AmitDiwan
Updated on 30-Sep-2019 07:27:00

4K+ Views

To suppress warnings, set SQL_NOTES=0. Let us see an example.At first, we will set SQL_NOTES to 1 −mysql> SET sql_notes = 1; Query OK, 0 rows affected (0.00 sec)Now, let us drop a table which does not exist. As you can see a warning message is now visible −mysql> drop table if exists web.DemoTable; Query OK, 0 rows affected, 1 warning (0.07 sec)To look at the above warning message, you need to just use the SHOW WARNINGS command −mysql> show warnings;This will produce the following output displaying the warning message −+-------+------+-----------------------------------+ | Level | Code | Message ... Read More

Sort values that contain letters and symbols in custom order with MySQL

AmitDiwan
Updated on 30-Sep-2019 07:25:28

195 Views

For custom order, use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable (    Title varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Java_1+'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('MySQL_23+'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('MongoDB++'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('C++_23'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

MySQL query to display records on the basis of conditions IS NULL OR !=1;?

AmitDiwan
Updated on 30-Sep-2019 07:23:28

110 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100),    Score int ); Query OK, 0 rows affected (1.10 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Score) values('John', 45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('Chris', null); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('David', null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name, Score) values('Bob', 1); Query OK, 1 row affected (0.11 sec)Display all records from ... Read More

Fetch datetime row from exactly past 7 days records in MySQL

AmitDiwan
Updated on 30-Sep-2019 07:15:19

749 Views

For this, you can use the INTERVAL 7 day concept. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    AdmissionDate datetime ); Query OK, 0 rows affected (0.83 sec)Note − Let’s say the current date is 2019-08-23.Insert some records in the table using insert command −mysql> insert into DemoTable(AdmissionDate) values('2019-01-23'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-15'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-16'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-24'); Query OK, 1 ... Read More

Advertisements