Found 6705 Articles for Database

How to insert a row with a timestamp “X days ago” in MySQL?

AmitDiwan
Updated on 31-Dec-2019 08:08:24

296 Views

To insert a row like this use the following syntax, wherein we are using CURRENT_TIMESTAMP −insert into yourTableName values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY);To understand the above syntax, let us create a table −mysql> create table DemoTable1983    (    DueDate timestamp    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1983 ... Read More

MySQL - changing table engine from innoDB to MyISAM?

AmitDiwan
Updated on 31-Dec-2019 08:06:19

349 Views

Let us first create a table −mysql> create table DemoTable1982    (    StudentId int    ,    StudentName varchar(20),    StudentAge int    ); Query OK, 0 rows affected (0.00 sec)Let us check the table engine type -mysql> show create table DemoTable1982;This will produce the following output −+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table         | Create Table                                                                                     ... Read More

MySQL query to calculate sum from 5 tables with a similar column named “UP”?

AmitDiwan
Updated on 31-Dec-2019 08:02:41

505 Views

For this, use UNION ALL along with SUM(). Let us create 5 tables −mysql> create table DemoTable1977    (    UP int    ); Query OK, 0 rows affected (0.00 sec) mysql> insert into DemoTable1977 values(10); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1977 values(20); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1977; +------+ | UP   | +------+ |   10 | |   20 | +------+ 2 rows in set (0.00 sec) mysql> create table DemoTable1978    (    UP int    ); Query OK, 0 rows affected (0.00 sec) mysql> ... Read More

Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL

AmitDiwan
Updated on 31-Dec-2019 07:59:15

2K+ Views

For this, use IF() with IS NULL property. Let us first create a table −mysql> create table DemoTable1976    (    FirstName varchar(20),    LastName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1976 values('John', 'Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1976 values('John', NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1976 values(NULL, 'Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1976 values('Chris', 'Brown'); Query OK, 1 row affected (0.00 sec)Display all records from the table ... Read More

MySQL query to count all the column values from two columns and exclude NULL values in the total count?

AmitDiwan
Updated on 31-Dec-2019 07:56:49

226 Views

Let us first create a table −mysql> create table DemoTable1975    (    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1975 values('John', 45); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1975 values('Chris', 67); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1975 values('David', 59); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1975 values('Bob', NULL); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1975;This ... Read More

Dynamic SQL to get a parameter and use it in LIKE for a new table created inside a stored procedure

AmitDiwan
Updated on 31-Dec-2019 07:54:52

516 Views

For this, use prepared statement. Let us first create a table −mysql> create table DemoTable1973    (    StudentId int,    StudentName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1973 values(101, 'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1973 values(102, 'John Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1973 values(103, 'David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1973 values(104, 'John Smith'); Query OK, 1 row affected (0.00 sec)Display all records from the table using ... Read More

MySQL query to get a specific row from rows

AmitDiwan
Updated on 31-Dec-2019 07:52:57

449 Views

Let us first create a table −mysql> create table DemoTable1972    (    Section char(1),    StudentName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1972 values('D', 'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1972 values('B', 'David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1972 values('A', 'Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1972 values('C', 'Carol'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1972;This ... Read More

MySQL procedure with SELECT to return the entire table

AmitDiwan
Updated on 31-Dec-2019 07:49:56

3K+ Views

Let us first create a table −mysql> create table DemoTable1971    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20),    StudentPassword int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1971(StudentName, StudentPassword) values('John', '123456'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1971(StudentName, StudentPassword) values('Chris', '123456'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1971(StudentName, StudentPassword) values('David', '123456'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1971(StudentName, StudentPassword) values('Mike', '123456'); Query OK, 1 row affected (0.00 sec)Display all ... Read More

Order by numeric value from string records separated by numbers like CSE 15, CSE 11, etc.?

AmitDiwan
Updated on 31-Dec-2019 07:45:16

105 Views

Let us first create a table −mysql> create table DemoTable1969    (    BranchCode varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1969 values('CSE 101'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 15'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 6'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 201'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Set a specific value for the first three column values in MySQL?

AmitDiwan
Updated on 31-Dec-2019 07:42:15

222 Views

To set a specific value for only 1st three values, you need to use LIMIT 3. Let us first create a table −mysql> create table DemoTable1968    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1968(Name) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('Sam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('Mike'); Query OK, 1 ... Read More

Advertisements