Found 4378 Articles for MySQL

Get the sum of multiple row (not all) values from a MySQL table?

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

729 Views

You can use aggregate function SUM() from MySQL for this. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Amount int ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Amount) values(400); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Amount) values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Amount) values(50); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Amount) values(500); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Amount) ... Read More

Can we ignore duplicate rows in COUNT?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

4K+ Views

Yes, we can ignore duplicate rows in COUNT using DISTINCT. Following is the syntax:select count(distinct yourColumnName) from yourTableName;In MySQL, COUNT() will display the number of rows. DISTINCT is used to ignore duplicate rows and get the count of only unique rows.Let us first create a table:mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(10) ); Query OK, 0 rows affected (0.47 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, ... Read More

How to search for a date in MySQL timestamp field?

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

664 Views

You can use DATE() function from MySQL for this. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentAdmissionDate timestamp ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentAdmissionDate) values('2011-01-12 12:34:43'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentAdmissionDate) values('2012-10-23 11:32:21'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentAdmissionDate) values('2001-02-14 05:12:01'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentAdmissionDate) values('2018-12-31 15:10:04'); Query OK, 1 row affected (0.22 sec) mysql> ... Read More

How to update a range of records in MySQL?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

743 Views

To update a range of records in MySQL, you can use BETWEEN. Let us first create a table:mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20),    Age int ); Query OK, 0 rows affected (0.53 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable(Name, Age) values('Larry', 23); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Age) values('Sam', 24); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name, Age) values('Chris', 21); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

MySQL query to list all the items in a group in one record?

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

1K+ Views

You can use GROUP_CONCAT() function to list all the items in a group in one record. Let us first create a table −mysql> create table DemoTable (    ProductId int,    ProductName varchar(40),    ProductCategory varchar(40) ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Product-1', '1Product'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(101, 'Product-2', '2Product'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(100, 'Product-1', '3Product'); Query OK, 1 row affected (0.14 sec)Following is the query to display all ... Read More

What is the most efficient way to check the presence of a row in a MySQL table?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

217 Views

The most efficient want to check the presence of a row, use the count():select count(1) from yourTableName where yourCondition;Let us first create a table:mysql> create table DemoTable (    Id int,    FirstName varchar(20) ); Query OK, 0 rows affected (0.73 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(100, 'Larry'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(110, 'Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(120, 'Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(130, 'Carol'); Query ... Read More

Turn on the general log in MySQL?

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

832 Views

To turn ON the general log, you need to use SET command. Following is the syntax −set global general_log=ON;Let us check the general log status with the help of SHOW command −mysql> SHOW variables like '%general_log%';This will produce the following output −+------------------+---------------------+ | Variable_name | Value | +------------------+---------------------+ | general_log | OFF | | general_log_file | DESKTOP-QN2RB3H.log | +------------------+---------------------+ 2 rows in set ... Read More

MySQL Stored Procedure DEFINER=`root`@`%` is not working in localhost?

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

1K+ Views

First of all, you need to check the host. The host can be ‘localhost’ or ‘%’. Check the existence of user accounts with host −mysql> select user, host from MySQL.user;This will produce the following output −+------------------+-----------+ | user             | host      | +------------------+-----------+ | Bob              | % | | User2            | % | | mysql.infoschema | % | ... Read More

Calculate age from date of birth in MySQL?

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

672 Views

To calculate age from date of birth, you can use the below syntax −select timestampdiff(YEAR, yourColumnName, now()) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentDOB datetime ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentDOB) values('1996-01-12'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentDOB) values('1990-12-31'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentDOB) values('1989-04-01'); Query OK, 1 row affected (0.45 sec) mysql> insert into DemoTable(StudentDOB) values('2000-06-17'); Query ... Read More

How to count null values in MySQL?

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

829 Views

To count null values in MySQL, you can use CASE statement. Let us first see an example and create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20) ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values(null); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(FirstName) values(''); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.17 ... Read More

Advertisements