Found 4381 Articles for MySQL

MySQL SUM function to add decimal values

AmitDiwan
Updated on 25-Sep-2019 12:12:07

1K+ Views

Let us first create a table −mysql> create table DemoTable (    Money DECIMAL(7, 2) ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100.67); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(199.33); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(500); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(400); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(800); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL query to select a record with two exact values?

AmitDiwan
Updated on 25-Sep-2019 12:08:56

206 Views

For this, you can use GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value int ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values(600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Value) values(600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Value) values(800); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Value) values(800); Query OK, 1 row affected (0.09 sec)Display all records from the table ... Read More

Display records on the basis of key-value pairs in MySQL

AmitDiwan
Updated on 25-Sep-2019 12:06:59

1K+ Views

For this, use JSON_OBJECTAGG(). Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(100),    Age int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'John', 23); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20, 'Carol', 21); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10, 'Sam', 24); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20, 'Chris', 20); Query OK, 1 row affected (0.13 sec)Display all records from the ... Read More

How to calculate time based on seconds in MySQL?

AmitDiwan
Updated on 25-Sep-2019 11:51:59

181 Views

Let us first create a table −mysql> create table DemoTable (    Logouttime time ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('5:50:00'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('6:10:10'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('8:00:50'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------+ | Logouttime | +------------+ | 05:50:00 | | 06:10:10 | | 08:00:50 ... Read More

MySQL query to count the number of 0s and 1s from a table column and display them in two columns?

AmitDiwan
Updated on 25-Sep-2019 11:49:21

598 Views

For this, you can use the aggregate function SUM(). Let us first create a table −mysql> create table DemoTable (    isMarried tinyint(1) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.19 sec) mysql> ... Read More

Convert VARCHAR Date to a different format in MySQL?

AmitDiwan
Updated on 25-Sep-2019 11:43:15

311 Views

Let’s say you have set dates in the VARCHAR format. Now if you want to update the format, then use the UPDATE command along with STR_TO_DATE(). The syntax is as follows −update yourTableName set yourColumnName=str_to_date(yourColumnName, '%m/%d/%Y');Let us first create a table −mysql> create table DemoTable (    DueDate varchar(100) ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('12/01/2019'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('01/31/2016'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('03/17/2018'); Query OK, 1 row affected (0.17 ... Read More

How to use COUNT() and IF() in a single MySQL query?

AmitDiwan
Updated on 25-Sep-2019 11:40:54

335 Views

Let us first create a table −mysql> create table DemoTable (    isValidUser boolean ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected ... Read More

Filter dates from a table with DATE and NULL records in MySQL

AmitDiwan
Updated on 25-Sep-2019 11:38:54

692 Views

Let us first create a table −mysql> create table DemoTable (    FirstDate datetime,    SecondDate datetime ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21', '2018-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-10-04', '2019-08-14'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-05-01', '2019-09-11'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable values(NULL, NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-03-01', NULL); Query OK, 1 row affected (0.13 sec)Display all records ... Read More

Dynamically choosing a column in MySQL?

AmitDiwan
Updated on 25-Sep-2019 11:36:44

1K+ Views

First, you need to prepare a query and then you need to execute the PREPARED statement to dynamically choose a column in MySQL.Let us first create a table −mysql> create table DemoTable (    EmployeeName varchar(100) ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John Doe'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------------+ | EmployeeName | +--------------+ | John Doe | +--------------+ 1 row in set ... Read More

How to filter dates in MySQL to fetch date record only for a specific month?

AmitDiwan
Updated on 25-Sep-2019 11:35:16

344 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate varchar(100) ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2018-01-21'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-08-13'); Query OK, 1 row affected (0.56 sec) mysql> insert into DemoTable values('2019-07-08'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2016-02-12'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------------+ | AdmissionDate | +---------------+ ... Read More

Advertisements