Found 4219 Articles for MySQLi

Finding total number of rows of tables across multiple databases in MySQL?

AmitDiwan
Updated on 13-Dec-2019 11:18:29

260 Views

To fetch total number of table rows across databases, use aggregate function SUM() along with INFORMATION SCHEMA. Let us first create a table, which is in “web” database −mysql> create table DemoTable1568    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1568 values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1568 values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1568 values('David'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select ... Read More

Getting the last 30 rows in MySQL

AmitDiwan
Updated on 13-Dec-2019 10:46:56

475 Views

To get the last 30 rows in MySQL, you need to use ORDER BY DESC and then LIMIT 30. The syntax is as follows −select * from yourTableName order by yourColumnName DESC LIMIT 30;Let us first create a table −mysql> create table DemoTable1567    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1567 values(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ... Read More

Using CASE statement in MySQL to display custom name for empty value

AmitDiwan
Updated on 13-Dec-2019 07:09:56

180 Views

For this, you can use CASE WHEN statement. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> ... Read More

Implement ORDER BY in MySQL to order records in human readable format?

AmitDiwan
Updated on 13-Dec-2019 07:08:14

71 Views

For this, use INET_ATON() in MySQL. Let’s say our records are in the form of an IP Address. The INET_ATON() method would allow a user to convert IP Address records to the number and then we can use ORDER BY to order them.Let us first create a table −mysql> create table DemoTable    -> (    -> IpAddress varchar(50)    -> ); Query OK, 0 rows affected (1.36 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('192.168.110.78'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('192.168.110.87'); Query OK, 1 row affected (0.27 ... Read More

MySQL query to display custom text for empty columns

AmitDiwan
Updated on 13-Dec-2019 07:06:24

169 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.13 sec)Display all records from the table ... Read More

Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query

AmitDiwan
Updated on 13-Dec-2019 07:04:30

170 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (3.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.78 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(70); ... Read More

Find second max in a table using MySQL query?

AmitDiwan
Updated on 13-Dec-2019 07:02:38

429 Views

You can use LIMIT 1 OFFSET 1. Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.92 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(2); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(204); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(5); Query OK, 1 row affected ... Read More

MySQL TINYINT type to return <>1 or IS NULL records

AmitDiwan
Updated on 13-Dec-2019 07:00:49

559 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY ,    -> EmployeeName varchar(20),    -> isMarried tinyint    -> ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName, isMarried) values('Chris', NULL); Query OK, 1 row affected (0.76 sec) mysql> insert into DemoTable(EmployeeName, isMarried) values('David', 1); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable(EmployeeName, isMarried) values('Mike', 0); Query OK, 1 row affected (0.69 sec) mysql> insert into DemoTable(EmployeeName, isMarried) values('Sam', NULL); Query OK, ... Read More

ERROR 1064 (42000): You have an error in your SQL syntax at zero fill column?

AmitDiwan
Updated on 13-Dec-2019 06:57:07

464 Views

Following is the error and it occurs when you implement ZEROFILL incorrectly−mysql> create table DemoTable    -> (    -> StudentCode int(10) NOT NULL ZEROFILL AUTO_INCREMENT PRIMARY KEY    -> ); 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 'ZEROFILL AUTO_INCREMENT PRIMARY KEY )' at line 3For correct implementation, use the below syntax −SyntaxyourColumnName int(10) ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEYLet us first create a table −mysql> create table DemoTable    -> (    -> StudentCode int(10) ZEROFILL NOT NULL ... Read More

Find integer in text data (comma separated values) with MySQL?

AmitDiwan
Updated on 13-Dec-2019 06:55:11

262 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> DoubleValue varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(DoubleValue) values('80.2, 90.5, 88.90'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable(DoubleValue) values('78.56, 45.80, 88, 45.6'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(DoubleValue) values('12.34, 90.06, 89.90'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will ... Read More

Advertisements