Found 4219 Articles for MySQLi

MySQL query to display only the column values with corresponding column having whitespace

AmitDiwan
Updated on 03-Sep-2019 12:19:19

165 Views

For this, use the TRIM() function. Let us first create a table −mysql> create table DemoTable826(    FirstName varchar(100),    Age int ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using the insert command. Here, we have set the FirstName as whitespace for some of the records −mysql> insert into DemoTable826 values(' ', 24); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable826 values(' ', 22); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable826 values('Chris', 26); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable826 values('Robert', 27); Query ... Read More

How to optimize a MySQL table after deleting some rows?

AmitDiwan
Updated on 03-Sep-2019 12:17:39

757 Views

Use the OPTIMIZE TABLE command to optimize a MySQL table −optimize table yourTableName;Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100) ); Query OK, 0 rows affected (1.38 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.38 sec)Display all records from ... Read More

Implement a MySQL Void Query that does nothing

AmitDiwan
Updated on 03-Sep-2019 12:12:28

200 Views

Let us first create a table −mysql> create table DemoTable(Name varchar(100)); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+ | Name   | +--------+ | Chris  | ... Read More

MySQL query to sum rows having repeated corresponding Id

AmitDiwan
Updated on 03-Sep-2019 14:39:00

213 Views

To sum, you can use aggregate function SUM(). Let us first create a table −mysql> create table DemoTable850(    Id int,    Price int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable850 values(1, 90); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable850 values(2, 100); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable850 values(2, 50); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable850 values(1, 80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable850 values(1, 60); Query OK, ... Read More

Updating last entry of a particular ID in MySQL

AmitDiwan
Updated on 03-Sep-2019 12:11:13

110 Views

To update the last entry of a particular ID in MySQL, use ORDER BY DESC LIMIT 1. This would allow you to update the last entry i.e. row records.Let us first create a table −mysql> create table DemoTable824(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(100),    ClientAge int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable824(ClientName, ClientAge) values('Chris', 23); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable824(ClientName, ClientAge) values('Robert', 26); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable824(ClientName, ... Read More

ORDER BY specific field value first in MySQL

AmitDiwan
Updated on 03-Sep-2019 11:58:33

307 Views

To order by specific field value first in MySQL, use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable849(Color varchar(100)); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable849 values('RED'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable849 values('ORANGE'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable849 values('BLUE'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable849 values('GREEN'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable849;This will ... Read More

Find records with a specific last digit in column with MySQL

AmitDiwan
Updated on 03-Sep-2019 11:56:39

985 Views

For this, use the RIGHT() method to fetch records with a specific last digit. Let us first create a table −mysql> create table DemoTable823(Value varchar(100)); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable823 values('9847826'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable823 values('84747464'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable823 values('9899889883'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable823 values('123456'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> select *from DemoTable823;This ... Read More

MySQL SELECT products WHERE 'average price per product' < value?

AmitDiwan
Updated on 03-Sep-2019 11:55:34

321 Views

Let us first create a table −mysql> create table DemoTable848(    ProductId int,    ProductPrice int ); Query OK, 0 rows affected (1.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable848 values(100, 30); Query OK, 1 row affected (0.57 sec) mysql> insert into DemoTable848 values(101, 50); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable848 values(100, 40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable848 values(101, 25); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable848 values(100, 20); Query OK, 1 row affected (0.31 sec)Display all records from ... Read More

ORDER BY a specific word in MySQL

AmitDiwan
Updated on 03-Sep-2019 11:53:51

378 Views

For this, use ORDER BY INSTR(). Let us first create a table −mysql> create table DemoTable822(Word text); Query OK, 0 rows affected (1.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable822 values('Forever'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable822 values('ever'); Query OK, 1 row affected (1.31 sec) mysql> insert into DemoTable822 values('every'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable822 values('everyday'); Query OK, 1 row affected (0.58 sec)Display all records from the table using select statement −mysql> select *from DemoTable822;This will produce the following output −+----------+ | Word ... Read More

MySQL query to exclude values having specific last 3 digits

AmitDiwan
Updated on 03-Sep-2019 11:52:11

214 Views

For this, use NOT IN. Let us first create a table −mysql> create table DemoTable(Value int); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1234); Query OK, 1 row affected (0.54 sec) mysql> insert into DemoTable values(2345); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(7896); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(4321); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | ... Read More

Advertisements