Found 4378 Articles for MySQL

MySQL query to group by column and display the sum of similar values in another column

AmitDiwan
Updated on 09-Sep-2019 08:12:49

427 Views

For this, use GROUP BY HAVING clause.Let us first create a table −mysql> create table DemoTable782 ( Name varchar(100), Score int ); Query OK, 0 rows affected (1.18 sec)Insert some records in the table using insert command −mysql> insert into DemoTable782 values('John', 156); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable782 values('Carol', 250); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable782 values('Bob', 140); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable782 values('John', 126); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable782 values('John', ... Read More

Find the count of EMPTY or NULL columns in a MySQL table?

AmitDiwan
Updated on 09-Sep-2019 08:10:53

980 Views

Let us first create a table −mysql> create table DemoTable781 ( Name varchar(100) ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable781 values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable781 values(''); Query OK, 1 ... Read More

How to display the column names from a table excluding some in MySQL?

AmitDiwan
Updated on 09-Sep-2019 08:09:06

316 Views

To exclude some of the column names, use NOT IN.Let us first create a table −mysql> create table DemoTable780 (    CustomerId int,    CustomerName varchar(100),    CustomerAge int, CustomerCountryName varchar(100), isMarried tinyint(1) ); Query OK, 0 rows affected (0.47 sec)Here is the query to exclude result −mysql> select group_concat(column_name) from `information_schema`.`COLUMNS` m where table_schema = 'web' and table_name = 'DemoTable780' and column_name not in ('CustomerId','CustomerCountryName') group by table_schema,table_name;This will produce the following output -+------------------------------------+ | group_concat(column_name) | +------------------------------------+ | CustomerName,CustomerAge,isMarried | +------------------------------------+ 1 row in set (0.01 sec)

Increment column value ‘ADD’ with MySQL SET clause

AmitDiwan
Updated on 09-Sep-2019 08:06:27

134 Views

Since the column value ‘ADD’ is already a reserved word, therefore you need to use backticks around the word ADD like `ADD`.Let us see an example and create a table −mysql> create table DemoTable779 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, `ADD` int ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable779(`ADD`) values(54); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable779(`ADD`) values(89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable779(`ADD`) values(66); Query OK, 1 row affected ... Read More

Select rows from a MySQL table and display using IN()

AmitDiwan
Updated on 09-Sep-2019 08:04:43

161 Views

Let us first create a table −mysql> create table DemoTable778 ( ClientId varchar(100), ClientName varchar(100) ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable778 values('J-101', 'John'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable778 values('A-102', 'Adam'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable778 values('C-103', 'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable778 values('D-104', 'David'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable778 values('R-105', 'Robert'); Query OK, 1 row affected ... Read More

MySQL query to display the column and its values using OR in WHERE statement

AmitDiwan
Updated on 09-Sep-2019 08:02:13

57 Views

Let us first create a table −mysql> create table DemoTable777 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentAge int ); Query OK, 0 rows affected (1.34 sec)Insert some records in the table using insert command −mysql> insert into DemoTable777(StudentName, StudentAge) values('Chris', 23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable777(StudentName, StudentAge) values('Robert', 21); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable777(StudentName, StudentAge) values('Mike', 19); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable777(StudentName, StudentAge) values('Bob', 22); Query OK, 1 row ... Read More

Why does the following error occur in MySQL: ERROR 1062 (23000): Duplicate entry?

AmitDiwan
Updated on 09-Sep-2019 07:59:17

3K+ Views

Let’s say you have set column values as unique key and try to insert duplicate values in the table. This will lead to ERROR 1062 (23000): Duplicate entry.Let us first create a table -mysql> create table DemoTable776 (    FirstValue int,    SecondValue int,    unique key(FirstValue, SecondValue) ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command. While inserting duplicate value, the same error arises as shown below -mysql> insert into DemoTable776 values(10, 20); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable776 values(10, 40); Query OK, 1 row affected (0.15 ... Read More

MySQL query to count records that begin with specific letters

AmitDiwan
Updated on 03-Jul-2020 12:02:02

263 Views

Let us first create a table −mysql> create table DemoTable775 ( FirstName varchar(100) ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable775 values('Adam') ; Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable775 values('Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable775 values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable775 values('Sam'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable775 values('Adan'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select ... Read More

Change the curdate() (current date) format in MySQL

AmitDiwan
Updated on 03-Sep-2019 14:23:43

8K+ Views

The current date format is ‘YYYY-mm-dd’. To change current date format, you can use date_format().Let us first display the current date −mysql> select curdate();This will produce the following output −+------------+ | curdate() | +------------+ | 2019-08-08 | +------------+ 1 row in set (0.00 sec)Following is the query to change curdate() (current date) format −mysql> select date_format(curdate(), '%m/%d/%Y');This will produce the following output −+------------------------------------+ | date_format(curdate(), '%m/%d/%Y') | +------------------------------------+ | 08/08/2019                         | +------------------------------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable ... Read More

MySQL query to multiply values of two rows and add the result

AmitDiwan
Updated on 03-Sep-2019 14:19:33

908 Views

For this, use aggregate function SUM(). Within this method, multiply the row values. Let us first create a table −mysql> create table DemoTable(    ProductQuantity int,    ProductPrice int ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 9); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(6, 20); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(7, 100); Query OK, 1 row affected (0.08 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

Advertisements