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
In this section we will see another sorting problem. Suppose we have two arrays A1 and A2. We have to sort A1 in such a way that the relative order among the elements will be same as those are in A2. If some elements are not present in A2, then they will be appended after the sorted elements. Suppose A1 and A2 are the following −A1 = {2, 1, 2, 1, 7, 5, 9, 3, 8, 6, 8} A2 = {2, 1, 8, 3}After the sorting A1 will be like below −A1 = {2, 2, 1, 1, 8, 8, 3, ... Read More
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
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
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
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
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
Here we will see one interesting problem to sort an array based on the set-bits. When an element in the array has higher number of set-bits, then that will be placed before another element which has lower number of set bits. Suppose some numbers are 12, 15, 7. So the set bits are basically number of 1’s in their binary representation. These are 1100 (12), 1111 (15), and 0111 (7). So after sorting it will be look like this −1111, 0111, 1100 (15, 7, 12)Here we have to find the number of set-bits at first. Then we will use the ... Read More
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 using insert command −mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (1.17 sec) mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Carol', 'Taylor'); Query OK, ... Read More
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> insert into DemoTable values('Adam', 'Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David', 'Chris'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+-------+ | Name1 | Name2 | +-------+-------+ | ... Read More