Found 4378 Articles for MySQL

How to check whether now() falls between two specific dates in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

402 Views

Here, now() represents the current date. To check whether it falls between two specific dates, you need to use the BETWEEN. Let us first create a table −mysql> create table DemoTable    (    FirstDate datetime,    SecondDate datetime    ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-04-01', '2019-05-02'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('2019-05-28', '2019-06-04'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('2016-01-31', '2019-03-01'); Query OK, 1 row affected (0.15 sec)Display all records from the table ... Read More

Get MAX() on column in two MySQL tables?

George John
Updated on 30-Jul-2019 22:30:26

405 Views

Use GREATEST() to find the maximum. Let us first create a table −mysql> create table DemoTable1    (    Number int    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(80); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1 values(229); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(575); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+--------+ | Number | +--------+ | 80 | | 229 ... Read More

Sorted difference between two columns in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

379 Views

Use ORDER BY clause for this. Let us first create a table −mysql> create table DemoTable    (    Value1 int,    Value2 int    ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 40); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(50, 5); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(51, 56); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(52, 78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(90, 7); Query ... Read More

MySQL select to count values equal to 0 and greater than 0 from a column?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

850 Views

For this, use the CASE statement. Let us first create a table −mysql> create table DemoTable    (    Number int    ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.09 sec) mysql> insert ... Read More

Can we update a row with the highest ID in a single MySQL query?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

183 Views

Yes, we can do that. Let us first create a table −mysql> create table DemoTable    (    ID int,    GameScore int    ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(15, 848747); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(13, 909049); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(34, 98474646); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(31, 948474); Query OK, 1 row affected (0.27 sec)Display all records from the table using select statement ... Read More

Resolve Syntax error near “ORDER BY order DESC” in MySQL?

George John
Updated on 30-Jul-2019 22:30:26

2K+ Views

The word order is a reserved order in MySQL and you have used it in the query. To get rid of the syntax error, you need to use backticks(` `) around the order.The correct syntax is as follows −select *from yourTableName ORDER BY `order` DESC;Let us first create a table −mysql> create table DemoTable    (    `order` int    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(67); Query OK, 1 row affected (0.13 sec) ... Read More

MySQL query to alphabetize records and count the duplicates?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

93 Views

For this, use both GROUP BY and ORDER BY clause. Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentGrade char(1)    ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentGrade) values('A'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentGrade) values('F'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentGrade) values('C'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(StudentGrade) values('A'); Query OK, 1 row affected (0.23 sec) mysql> insert ... Read More

Convert the column to a case-sensitive collation in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

497 Views

For this, you can use COLLATE. Following is the syntax −select *from yourTableName where yourColumnName LIKE yourValue COLLATE utf8_bin;Let us first create a table −mysql> create table DemoTable    (    LastName varchar(100)    ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('BROWN'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('BRoWN'); Query OK, 1 row affected (0.14 sec)Display all records ... Read More

MySQL query to decrease value by 10 for a specific value?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

497 Views

Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score int    ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Score) values(67); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Score) values(78); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Score) values(90); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Score) values(56); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+----+-------+ ... Read More

Remove last char if it's a specific character in MySQL?

George John
Updated on 30-Jul-2019 22:30:26

1K+ Views

To remove last char if it is a specific character then use SUBSTRING(). Let us first create a table −mysql> create table DemoTable    (    SubjectName varchar(100)    ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('MongoDB?'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Java?'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('C'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select ... Read More

Advertisements