Found 4378 Articles for MySQL

Want to fetch only the month part from a date in MySQL

AmitDiwan
Updated on 03-Sep-2019 07:50:04

86 Views

To fetch only the month part, use DATE_FORMAT().Let us first create a table −mysql> create table DemoTable753 (DueDate datetime); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable753 values('2019-06-21'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable753 values('2019-01-31'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable753 values('2018-12-01'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable753 values('2016-11-11'); Query OK, 1 row affected (0.23 sec)Display all records from the table using select statement −mysql> select *from DemoTable753;This will produce the following output -+---------------------+ ... Read More

MySQL query to ORDER BY `user_id` IN (1,2,3) AND `name` for custom ordering

AmitDiwan
Updated on 03-Sep-2019 07:47:38

95 Views

To implement IN() for custom ordering, use ORDER BY CASE.Let us first create a table −mysql> create table DemoTable752 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100) ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable752(Name) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable752(Name) values('Carol'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable752(Name) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable752(Name) values('Mike'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable752(Name) values('Sam'); ... Read More

Get all the records with two different values in another column with MySQL

AmitDiwan
Updated on 03-Sep-2019 07:43:13

594 Views

For this, you can use GROUP BY HAVING clause.Let us first create a table −mysql> create table DemoTable751 (    StudentName varchar(100),    SubjectName varchar(100) ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable751 values('John', 'MySQL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable751 values('John', 'MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable751 values('Sam', 'MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable751 values('Carol', 'Java'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable751 values('David', 'MySQL'); ... Read More

Select records from a table on the basis of keywords in MySQL

AmitDiwan
Updated on 03-Sep-2019 07:40:36

394 Views

Let’s say some of the columns values in a table has a specific keyword and you want only those records. For this, use the LIKE operator.Let us first see an example and create a table −mysql> create table DemoTable750 (Title varchar(200)); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable750 values('Java and MongoDB, MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable750 values('MySQL, SQL Server'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable750 values('PL/SQL, Python, MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> ... Read More

List logged-in MySQL users?

AmitDiwan
Updated on 03-Sep-2019 07:38:50

137 Views

To list logged-in MySQL users, you can use any of the following two methods −First MethodUse INFORMATION_SCHEMA.PROCESSLISTselect *from INFORMATION_SCHEMA.PROCESSLIST;Second MethodYou can use SHOW PROCESSLIST command as well. Following is the syntax −SHOW PROCESSLIST;Let us implement the above syntaxes in order to list logged in MySQL users −mysql> select *from information_schema.processlist;This will produce the following output -+----+-----------------+-----------------+------+---------+--------+-----------------------------+---------------------------------------------+ | ID | USER            | HOST            | DB   | COMMAND | TIME   | STATE                       | INFO           ... Read More

Update all the zero values with a custom value in MySQL with a function similar to ISNULL()

AmitDiwan
Updated on 03-Sep-2019 07:36:20

272 Views

For this, you can use custom IF() and set a value whenever 0 appears.Let us first create a table −mysql> create table DemoTable749 (Value int); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable749 values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable749 values(0); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable749 values(769); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable749 values(0); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable749 values(78); Query OK, 1 row affected (0.14 sec) ... Read More

MySQL query to extract only the day instead of entire date

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

579 Views

To extract only the day instead of the entire date, you need to use DAYOFMONTH() function from MySQL.Let us first create a table −mysql> create table DemoTable747 (DueDate datetime); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable747 values('2019-01-31') ; Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable747 values('2018-12-01'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable747 values('2017-09-14'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable747 values('2016-07-21'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select ... Read More

How to update records in a column with random numbers in MySQL?

AmitDiwan
Updated on 03-Jul-2020 07:24:13

168 Views

Let us first create a table −mysql> create table DemoTable746 (    Number int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable746 values(100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable746 values(200); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable746 values(300); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable746 values(400); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable746;This will produce the following output -+--------+ | Number | +--------+ ... Read More

MySQL query to display only the empty and NULL values together?

AmitDiwan
Updated on 26-Aug-2019 08:52:04

180 Views

To check for NULL, use the IS NULL. For empty values, you need to check with an empty string. We will now see an example.Let us first create a table −mysql> create table DemoTable691(    PlayerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    PlayerName varchar(100),    PlayerScore int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable691(PlayerName, PlayerScore) values('Robert', 56); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable691(PlayerName, PlayerScore) values('David', 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable691(PlayerName, PlayerScore) values('', 98); Query ... Read More

Will “create table table” work in MySQL since we cannot use reserved words as table name?

AmitDiwan
Updated on 26-Aug-2019 08:45:55

93 Views

Let us first see a case wherein we use “create table table” while creating a table. An error will arise −mysql> create table table(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) );This will produce the following output i.e. error −ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) )' at line 1As you can see above, the word “table” is a reserved keyword, and we ... Read More

Advertisements