Found 4378 Articles for MySQL

How to use an OUT parameter / read data with SELECT from table in a MySQL procedure?

AmitDiwan
Updated on 26-Dec-2019 06:46:30

419 Views

For this, you can use SELECT INTO. Let us first create a table −mysql> create table DemoTable1860      (      Amount int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1860 values(1590); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1860 values(410); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1860 values(3000); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −Mysql> select * from DemoTable1860; This will produce the following output −+--------+ | Amount ... Read More

Is there an easy way to rename a table in a MySQL procedure?

AmitDiwan
Updated on 26-Dec-2019 06:44:11

180 Views

Yes, use the ALTER command with RENAME. Let us first create a table −mysql> create table DemoTable1859      (      Id int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1859 values(101); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1859 values(102); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1859; This will produce the following output −+------+ | Id   | +------+ |  101 | |  102 | +------+ 2 rows ... Read More

Find duplicate column values in MySQL and display them

AmitDiwan
Updated on 26-Dec-2019 06:39:26

505 Views

For this, use GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable1858      (      ModelNumber varchar(50)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1858 values('Audi A4'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi A6'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi A4'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi Q5'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi R8'); ... Read More

MySQL query to get all characters before a specific character hyphen

AmitDiwan
Updated on 26-Dec-2019 06:38:27

798 Views

For this, you can use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable1857      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1857 values('John-Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1857 values('Brown-Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1857 values('David-Carol-Miller'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1857; This will produce the following output −+--------------------+ | Name   ... Read More

Using GROUP_CONCAT() on bit fields returns garbage in MySQL? How to fix?

AmitDiwan
Updated on 26-Dec-2019 06:37:09

124 Views

To fix, use group_concat() with addition of 0 with column. Let us first create a table −mysql> create table DemoTable1856      (      Id int,      Value bit(1)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1856 values(101, 1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1856 values(102, 0); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1856 values(101, 0); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1856 values(102, 1); Query OK, 1 row affected (0.00 ... Read More

Which MySQL Datatype should be used for storing BloodType?

AmitDiwan
Updated on 26-Dec-2019 06:35:52

812 Views

To store BloodType, use varchar(3) or ENUM. Let us first create a table −mysql> create table DemoTable1855      (      BloodType varchar(3)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1855 values('A+'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('A-'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('B+'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('B-'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('AB+'); Query OK, 1 row affected ... Read More

Distinct number of specific items in list with MySQL

AmitDiwan
Updated on 26-Dec-2019 06:34:03

145 Views

To find distinct number of specific items, use COUNT() along with GROUP BY clause. Let us first create a table −mysql> create table DemoTable1854      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1854 values('John-Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('Chris-Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('Adam-Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('John-Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Select only 5 random rows in the last 50 entries With MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:31:46

296 Views

For this, use ORDER BY RAND() with subquery. Let us first create a table −mysql> create table DemoTable1853      (      UserId int NOT NULL AUTO_INCREMENT,      PRIMARY KEY(UserId)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1853 values(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ... Read More

Why does comparing types in MySQL won’t raise an error?

AmitDiwan
Updated on 26-Dec-2019 06:30:23

85 Views

If you try to compare string to int, MySQL won’t raise an error because it converts string to int. Let us first create a table −mysql> create table DemoTable1852      (      Value1 varchar(20),      Value2 int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1852 values('1John', 1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1852 values('John', 1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1852 values('1', 1); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Delete records where timestamp older than 5 minutes in MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:28:28

2K+ Views

For this, use DELETE command. Let us first create a table −mysql> create table DemoTable1851      (      DueDate datetime      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1851 values('2019-12-03 21:30:35'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1851 values('2019-12-03 21:45:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1851 values('2019-12-03 21:34:00'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1851; This will produce the following output −+---------------------+ | ... Read More

Advertisements