Found 4219 Articles for MySQLi

Add a column count in a MySQL query on the basis of last name records?

AmitDiwan
Updated on 30-Sep-2019 07:29:38

354 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (1.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName) values('David', 'Miller'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable(FirstName, LastName) values('Carol', 'Miller'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable(FirstName, LastName) values('John', 'Doe'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

How to suppress warnings in MySQL?

AmitDiwan
Updated on 30-Sep-2019 07:27:00

3K+ Views

To suppress warnings, set SQL_NOTES=0. Let us see an example.At first, we will set SQL_NOTES to 1 −mysql> SET sql_notes = 1; Query OK, 0 rows affected (0.00 sec)Now, let us drop a table which does not exist. As you can see a warning message is now visible −mysql> drop table if exists web.DemoTable; Query OK, 0 rows affected, 1 warning (0.07 sec)To look at the above warning message, you need to just use the SHOW WARNINGS command −mysql> show warnings;This will produce the following output displaying the warning message −+-------+------+-----------------------------------+ | Level | Code | Message ... Read More

Sort values that contain letters and symbols in custom order with MySQL

AmitDiwan
Updated on 30-Sep-2019 07:25:28

131 Views

For custom order, use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable (    Title varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Java_1+'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('MySQL_23+'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('MongoDB++'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('C++_23'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

MySQL query to display records on the basis of conditions IS NULL OR !=1;?

AmitDiwan
Updated on 30-Sep-2019 07:23:28

79 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100),    Score int ); Query OK, 0 rows affected (1.10 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Score) values('John', 45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('Chris', null); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('David', null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name, Score) values('Bob', 1); Query OK, 1 row affected (0.11 sec)Display all records from ... Read More

Fetch datetime row from exactly past 7 days records in MySQL

AmitDiwan
Updated on 30-Sep-2019 07:15:19

557 Views

For this, you can use the INTERVAL 7 day concept. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    AdmissionDate datetime ); Query OK, 0 rows affected (0.83 sec)Note − Let’s say the current date is 2019-08-23.Insert some records in the table using insert command −mysql> insert into DemoTable(AdmissionDate) values('2019-01-23'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-15'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-16'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-24'); Query OK, 1 ... Read More

Fetching records from a table with NULL and other values on the basis of conditions in MySQL

AmitDiwan
Updated on 30-Sep-2019 07:12:36

91 Views

Let us first create a table −mysql> create table DemoTable (    value1 int,    value2 int,    value3 int ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20, 40, null); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(40, 40, null); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(null, null, null); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+--------+--------+ | value1 | value2 ... Read More

MySQL query to ORDER BY records on the basis of modulus result

AmitDiwan
Updated on 30-Sep-2019 07:08:44

98 Views

For this, use ORDER BY with a modulus operator. Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentName varchar(100) ); Query OK, 0 rows affected (1.88 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(101, 'Robert'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(103, 'Mike'); Query OK, 1 row affected (0.23 sec)Display all records from the table using ... Read More

MySQL multiple COUNT with multiple columns?

AmitDiwan
Updated on 30-Sep-2019 07:05:35

298 Views

You can use an aggregate function SUM() along with IF(). Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (2.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('John', 'Smith'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (1.38 sec) mysql> insert into DemoTable values('Bob', 'Doe'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Sam', ... Read More

Search text containing a new line in MySQL?

AmitDiwan
Updated on 06-Jul-2020 06:00:18

560 Views

You can use REGEXP. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (1.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('JohnSmith'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('John Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('DavidMiller'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.27 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

Use LIKE % to fetch multiple values in a single MySQL query

AmitDiwan
Updated on 30-Sep-2019 07:00:04

3K+ Views

To fetch multiple values wit LIKE, use the LIKE operator along with OR operator. Let us first create a table −mysql> create table DemoTable1027 (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (1.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1027 values(100, 'John'); Query OK, 1 row affected (0.72 sec) mysql> insert into DemoTable1027 values(20, 'Chris'); Query OK, 1 row affected (0.56 sec) mysql> insert into DemoTable1027 values(200, 'Robert'); Query OK, 1 row affected (0.84 sec) mysql> insert into DemoTable1027 values(400, 'Mike'); Query OK, 1 row affected (0.47 sec)Display all ... Read More

Advertisements