Found 4219 Articles for MySQLi

Trying to round a calculation to two decimal places in a new column with MySQL?

AmitDiwan
Updated on 26-Dec-2019 07:18:00

434 Views

To round, use MySQL ROUND() function. Let us first create a table −mysql> create table DemoTable1865      (      Value1 int,      Value2 int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1865 values(40, 60); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1865 values(100, 400); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1865 values(545, 896); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1865;This will produce the ... Read More

How to assign the result of a MySQL query into a variable?

AmitDiwan
Updated on 26-Dec-2019 07:16:28

849 Views

Use @anyVariableName to assign the result of a query into a variable. Let us first create a table −mysql> create table DemoTable1864      (      Id int,      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 DemoTable1864 values(101, 'Chris', 'Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1864 values(102, 'David', 'Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1864 values(103, 'Adam', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

How to set default value for empty row in MySQL?

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

551 Views

To set default value for empty row, use the concept of COALESCE(). Let us first create a table −mysql> create table DemoTable1863      (      FirstName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1863 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values(NULL); Query OK, 1 row affected (0.00 sec)Display all records from the table ... Read More

How to find records with a null value in a set of columns with MySQL

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

149 Views

For this, use the concept of GREATEST(). Let us first create a table −mysql> create table DemoTable1862      (      Value1 int,      Value2 int,      Value3 int,      Value4 int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1862 values(43, 34, 56, 42); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1862 values(NULL, 78, 65, NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1862 values(110, NULL, 78, NULL); Query OK, 1 row affected (0.00 sec)Display all ... Read More

How to find rows with exact value in one or more columns with MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:48:18

350 Views

For this, you can use GROUP BY HAVING with subquery. Let us first create a table −mysql> create table DemoTable1861      (      Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,      Name varchar(20),      Marks int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1861(Name, Marks) values('John', 45); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1861(Name, Marks) values('Chris', 74); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1861(Name, Marks) values('David', 89); Query OK, 1 row affected (0.00 sec) ... Read More

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

506 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

Advertisements