Found 4219 Articles for MySQLi

How to generate 6-digit random number in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

2K+ Views

You can use LPAD() along with rand() and floor() to generate 6-digit random number. Let us first create a table −mysql> create table DemoTable    (    Value int    ); Query OK, 0 rows affected (0.64 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(2); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(3); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(5); Query OK, ... Read More

How to validate expiry date on a MySQL query?

George John
Updated on 30-Jul-2019 22:30:26

2K+ Views

You can use NOW() for this. Following is the syntax −select * from yourTableName where yourColumnName> now();Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    expiryDateOfMedicine datetime    ); Query OK, 0 rows affected (0.55 sec)Insert records in the table using insert command −mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-27 11:29:00'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-26 10:39:21'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-28 11:30:10'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-29 12:44:11'); ... Read More

Select the table name as a column in a UNION select query with MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

436 Views

You can use AS command for this. Let us first create a table −mysql> create table DemoTable    (    Name varchar(20)    ); Query OK, 0 rows affected (0.56 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce the following output −+------+ | Name | +------+ | John | +------+ 1 row in set (0.00 sec)Here is the query to create the second table.mysql> create table DemoTable2    (    Name varchar(20)   ... Read More

MySQL query to get a field value that does not contain empty spaces?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

365 Views

Use NOT LIKE for this. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFullName varchar(40)    ); Query OK, 0 rows affected (0.66 sec)Insert records in the table using insert command −mysql> insert into DemoTable(StudentFullName) values('JohnSmith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentFullName) values('John Doe'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentFullName) values('Adam Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentFullName) values('CarolTaylor'); Query OK, 1 row affected (0.19 sec)Display all records from the table using ... Read More

How to count the distinct column in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

141 Views

You need to use GROUP BY for this. Let us first create a table −mysql> create table DemoTable    (    StudentFirstName varchar(20)    ); Query OK, 0 rows affected (0.74 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (1.34 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.25 sec) mysql> ... Read More

How to understand if a bigint is signed or unsigned in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

1K+ Views

If you do not specify unsigned, then bigint will be signed. If you specify an unsigned, then bigint will be unsigned.Let us first create a table −mysql> create table DemoTable    (    Number bigint, // signed    Number2 bigint unsigned // unsigned    ); Query OK, 0 rows affected (1.08 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(18446744073709551615, 18446744073709551615); ERROR 1264 (22003): Out of range value for column 'Number' at row 1 mysql> insert into DemoTable values(9223372036854775807, 18446744073709551615); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> ... Read More

How to check if a table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

905 Views

In order to check if a table already exists in the database by using MySQL, you can use INFORMATION_SCHEMA.TABLES. Following is the syntax −SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘yourDatabaseName’ AND TABLE_NAME = ’yourTableName’;Let us implement the above syntax in order to check if a table already exists in the database.Case 1: When the table is present −mysql> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sample' AND TABLE_NAME = 'DemoTable';This will produce the following output −+--------------+ | TABLE_NAME   | +--------------+ | DemoTable | +--------------+ 1 row in set (0.01 sec)Case 2: When the table is ... Read More

MySQL query to select all fields beginning with a given number with next character a letter?

George John
Updated on 30-Jul-2019 22:30:26

301 Views

You can use REGEXP for this. Let us first create a table −mysql> create table DemoTable    (    Value text    ); Query OK, 0 rows affected (1.28 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('645st'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('765stp'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('665tcp'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('606cpp'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce ... Read More

How to search a word by capital or small letter in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

341 Views

You can use BINARY along with the LIKE operator. Let us first create a table −mysql> create table DemoTable    (    Header text    ); Query OK, 0 rows affected (1.09 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('Programming tutorials on MySQL'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('programming in Java language'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Program in C language'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('pRograMMing in Python language'); Query OK, 1 row affected (0.41 sec)Display ... Read More

Counting same strings in a new MySQL column?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

82 Views

Use COUNT() for this. Let us first create a table −mysql> create table DemoTable    (    StudentFirstName varchar(20)    ); Query OK, 0 rows affected (0.53 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Larry'); ... Read More

Advertisements