Set Conditions for Columns with Values 0 or 1 in MySQL

AmitDiwan
Updated on 26-Feb-2020 05:36:57

742 Views

To set conditions, use CASE WHEN statement in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Value1 int,    -> Value2 int,    -> Value3 int,    -> Value4 int    -> ); Query OK, 0 rows affected (0.98 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 0, 1, 1); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(1, 0, 1, 0); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1, 1, 1, 1); Query OK, 1 row affected (0.20 ... Read More

Change MySQL Ending Statement

AmitDiwan
Updated on 26-Feb-2020 05:36:06

196 Views

To change the MySQL ending statement, you can use DELIMITER −DELIMITER anySymbolAbove, anySymbol is the symbol you can set. The default is DELIMITER ;Let us first create a table −mysql> DELIMITER // mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> )// Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris')// Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(101, 'David')// Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(102, 'Bob')// Query OK, 1 ... Read More

Find Nth Maximum Value in MySQL

AmitDiwan
Updated on 26-Feb-2020 05:34:15

166 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(85); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.10 sec)Display all records from the table ... Read More

Batch Update MySQL Table

AmitDiwan
Updated on 26-Feb-2020 05:27:26

528 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> BreakfastTime time    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('7:30:45'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('8:00:30'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('7:55:00'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce the following output −+---------------+ | BreakfastTime | +---------------+ | 07:30:45      | | ... Read More

Implement SELECT, LIKE and CHAR_LENGTH in a Single MySQL Query

AmitDiwan
Updated on 26-Feb-2020 05:26:32

132 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Adam Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Carol Smith'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This ... Read More

Update All Fields in a Table with NULL or Non-NULL Values in MySQL

AmitDiwan
Updated on 26-Feb-2020 05:25:47

755 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, NULL); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(NULL, 'David'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(NULL, NULL); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce the following output−+------+-------+ |   Id | ... Read More

Store Double and Date in Varchar with MySQL

AmitDiwan
Updated on 26-Feb-2020 05:24:46

215 Views

Yes, you can store double and date in VARCHAR. Let us first create a table −mysql> create table DemoTable    -> (    -> Amount varchar(20),    -> JoiningDate varchar(20)    -> ); Query OK, 0 rows affected (0.96 sec)Insert some records in the table using insert command. We are inserting varchar double and date values −mysql> insert into DemoTable values('150.50', 'Oct 10, 2019'); Query OK, 1 row affected (0.91 sec) mysql> insert into DemoTable values('173.45', 'Sept 11, 2018'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('173.90', 'Jan 01, 2017'); Query OK, 1 row affected (0.16 ... Read More

Sum Corresponding Duplicate Records in MySQL

AmitDiwan
Updated on 26-Feb-2020 05:23:52

605 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentName varchar(20),    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 50); Query OK, 1 row affected (0.61 sec) mysql> insert into DemoTable values('David', 70); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('Chris', 80); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David', 90); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> ... Read More

Find Duplicate Tuples and Display Count in MySQL

AmitDiwan
Updated on 26-Feb-2020 05:22:43

544 Views

To find duplicate tuples, use GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(101, 'David'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(101, 'Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(100, 'Carol'); Query OK, 1 row affected (0.17 sec) ... Read More

Add Not Null Column to Existing Table with Records

AmitDiwan
Updated on 26-Feb-2020 05:20:44

285 Views

To add a new NOT NULL column to an already created table, use ALTER command. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.60 sec)Following is the query to add a new NOT NULL column to an existing table −mysql> alter table DemoTable add column StudentAge int NOT NULL; Query OK, 0 rows affected (0.52 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, ... Read More

Advertisements