Found 4219 Articles for MySQLi

Perform multiple counting without using MySQL COUNT()?

AmitDiwan
Updated on 10-Dec-2019 08:17:27

149 Views

To count, you can use SUM() along with CASE statement for conditions. Let us first create a table −mysql> create table DemoTable1485    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentSubject varchar(20)    -> ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1485(StudentName, StudentSubject) values('Chris', 'MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1485(StudentName, StudentSubject) values('Robert', 'MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1485(StudentName, StudentSubject) values('Robert', 'MongoDB'); Query OK, 1 row affected ... Read More

Insert MAX(col)+1 into the same MySQL table?

AmitDiwan
Updated on 10-Dec-2019 08:15:01

407 Views

Let us first create a table −mysql> create table DemoTable1484    -> (    -> Id int    -> ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1484 values(100); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1484 values(175); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1484 values(165); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1484 values(145); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1484 values(170); Query OK, 1 row affected (0.10 sec)Display all records from the table ... Read More

Perform MySQL SELECT INTO user-defined variable

AmitDiwan
Updated on 10-Dec-2019 08:11:30

167 Views

Let us first create a table −mysql> create table DemoTable1483    -> (    -> Salary int    -> ); Query OK, 0 rows affected (0.41 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1483 values(100); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1483 values(500); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1483 values(400); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable1483;This will produce the following output −+--------+ | Salary | +--------+ |    100 | |   ... Read More

How to display the result more readable if the string is long stored in MySQL VARCHAR?

AmitDiwan
Updated on 10-Dec-2019 08:09:14

371 Views

For this, use \G as in the below syntax −select * from yourTableName\GLet us first create a table −mysql> create table DemoTable1482    -> (    -> Title varchar(255)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1482 values('Deep Dive using Java with Data Structure And Algorithm'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1482 values('Introduction To MySQL and MongoDB'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable1482;This will produce the following ... Read More

How to replace rows in a MySQL table with conditions?

AmitDiwan
Updated on 10-Dec-2019 08:06:43

161 Views

To set conditions and replace rows, use MySQL CASE statement. Let us first create a table −mysql> create table DemoTable1481    -> (    -> PlayerScore int    -> ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1481 values(454); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable1481 values(765); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1481 values(890); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select * from DemoTable1481;This will produce the following output −+-------------+ ... Read More

How do I write not greater than in a MySQL query?

AmitDiwan
Updated on 10-Dec-2019 08:03:33

810 Views

The not greater than in a query can be written simply like less than or equal to ( (    -> StudentName varchar(40),    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1480 values('Chris', 78); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1480 values('Bob', 45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1480 values('John', 67); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1480 values('Adam', 56); Query OK, 1 row affected (0.14 sec)Display all records ... Read More

Can we insert records in a MySQL table without auto_increment values?

AmitDiwan
Updated on 10-Dec-2019 08:00:33

1K+ Views

Yes, we can insert without the auto_increment since it gets inserted on its own. Let us first create a table −mysql> create table DemoTable1479    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeSalary int    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1479(EmployeeSalary) values(6800); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1479(EmployeeSalary) values(5600); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1479(EmployeeSalary) values(5700); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1479(EmployeeSalary) values(6900); Query ... Read More

Count NOT NULL values from separate tables in a single MySQL query

AmitDiwan
Updated on 10-Dec-2019 07:58:11

128 Views

To count values from separate tables, the syntax is as follows −Select    (    select count(yourColumnName) from yourTableName1) as anyAliasName1,    (    select count(yourColumnName) from yourTableName2) as anyAliasName2;Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int    -> ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1 values(NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(2); Query OK, 1 row affected (0.34 sec) ... Read More

Reorder keys after deleting a record from MySQL table?

AmitDiwan
Updated on 10-Dec-2019 07:53:47

406 Views

For this, use UPDATE command with some mathematical calculations. To delete an id, use the DELETE. Let us first create a table −mysql> create table DemoTable1476    -> (    -> Id int    -> ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1476 values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1476 values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1476 values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1476 values(40); Query OK, 1 row affected (0.12 sec)Display all ... Read More

A single MySQL select query on two tables is possible?

AmitDiwan
Updated on 10-Dec-2019 07:49:57

190 Views

Yes, it is possible. Following is the syntax −select * from yourTableName1, yourTableName2;Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(), (), (); Query OK, 3 rows affected (0.14 sec) Records: 3  Duplicates: 0  Warnings: 0Display all records from the table using select statement −mysql> select * from DemoTable1;This will produce the following output −+----+ | Id | +----+ |  1 | |  2 ... Read More

Advertisements