AmitDiwan has Published 10744 Articles

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

AmitDiwan

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 ... Read More

Convert VARCHAR Date to a different format in MySQL?

AmitDiwan

AmitDiwan

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

309 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) ); ... Read More

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

AmitDiwan

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 ... Read More

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

AmitDiwan

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 ... Read More

Dynamically choosing a column in MySQL?

AmitDiwan

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 ... Read More

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

AmitDiwan

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 ... Read More

MySQL GROUP BY and CONCAT() to display distinct first and last name

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:30:57

1K+ Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (0.92 sec) mysql> alter table DemoTable add index(FirstName, LastName); Query OK, 0 rows affected (1.00 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table ... Read More

Select distinct names from two columns in MySQL and display the result in a single column

AmitDiwan

AmitDiwan

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

647 Views

For this, use UNION. Let us first create a table −mysql> create table DemoTable (    Name1 varchar(100),    Name2 varchar(100) ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert commandmysql> insert into DemoTable values('Adam', 'Bob'); Query OK, 1 row affected (0.21 sec) mysql> ... Read More

Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:11:22

451 Views

For this, use a CASE statement. Let us first create a table −mysql> create table DemoTable (    Value char(1) ); Query OK, 0 rows affected (1.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('a'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

Can we use INTERVAL keyword while inserting date records in a MySQL table?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:09:28

272 Views

Yes, we can use INTERVAL while inserting data records. Let us first create a table −mysql> create table DemoTable (    ArrivalTime datetime ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command. Here, we are using INTERVAL keyword for incrementing the date records ... Read More

Advertisements