MySQL Articles

Page 161 of 355

How to replace 'Empty set' in a MySQL query?

AmitDiwan
AmitDiwan
Updated on 13-Dec-2019 769 Views

To replace a record that doesn’t exist, use the COALESCE in MySQL. The COALESCE would help in substituting the NULL values. Let us first create a table −mysql> create table DemoTable    -> (    -> Code varchar(20)    -> ); Query OK, 0 rows affected (1.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('45'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('78'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement ...

Read More

Order by a single field and display rest of the records in the same order with MySQL

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 241 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(201, 'Chris Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(110, 'John Doe'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values(101, 'Adam Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(345, 'Carol Taylor'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(135, ...

Read More

Find "greatest" between two columns and display with some records already null in MySql

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 176 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value1 int,    -> Value2 int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(78, 89); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(19, null); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(null, 0); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(null, 95); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> ...

Read More

Order records and delete n rows in MySQL

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 202 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 ...

Read More

Add a single day to datetime field with MySQL INTERVAL

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 208 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate date    ->); Query OK, 0 rows affected (2.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values('2018-12-30'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('2017-02-26'); Query OK, 1 row affected (0.47 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------+ ...

Read More

Shifting values of rows in MySQL to change the existing id values for existing rows?

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 533 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (1.07 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName) values('Chris'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(StudentName) values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName) values('David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentName) values('Mike'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement ...

Read More

MySQL query to delete last two words from every column value

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 215 Views

For this, you can use the LEFT() function from MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(10)    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('Robert'); 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 following output −+--------+ | ...

Read More

Can we use the word user for a MySQL table?

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 399 Views

You cannot use “user” for a MySQL table because it is a reserved word in MySQL. You can change the name from user to users or something else or you can use backticks around the user word.The word user can be used to create a user or can select a user list from the MySQL database.Let us first create a table. Here, we have used the table name users −mysql> create table users    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20),    -> UserAge int    -> ); Query OK, 0 rows ...

Read More

MySQL Select where timestamp is in the current hour?

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 272 Views

For this, you can use CURTIME(). Let us first create a table −mysql> create table DemoTable    -> (    -> ArrivalTime timestamp    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-10-26 17:55:55'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('2019-10-26 18:00:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-10-26 18:55:00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-10-26 16:00:10'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select ...

Read More

How to declare a variable inside a procedure in MySQL?

AmitDiwan
AmitDiwan
Updated on 12-Dec-2019 2K+ Views

You can use the DECLARE command to declare a variable inside a MySQL procedure. Let us create a stored procedure in MySQL −mysql> DELIMITER // mysql> CREATE PROCEDURE DECLARE_VARIABLE_DEMO(IN value int)    -> BEGIN    -> DECLARE searchValue int;    -> set searchValue=value;    -> if searchValue=10 then    ->    select searchValue+100;    -> else    ->    select searchValue;    -> end if;    -> END    -> // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;Above, we have declared a variable. Now, let us call the stored procedure using CALL command −mysql> call DECLARE_VARIABLE_DEMO(10);This will ...

Read More
Showing 1601–1610 of 3,547 articles
« Prev 1 159 160 161 162 163 355 Next »
Advertisements