Count Values Yes/No with Same IDs in MySQL

AmitDiwan
Updated on 12-Nov-2019 06:28:38

752 Views

For this, you can use SUM() along with CASE statement. Let us first create a −mysql> create table DemoTable1430    -> (    -> EmployeeId int,    -> isMarried ENUM('YES', 'NO')    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert −mysql> insert into DemoTable1430 values(1001, 'Yes'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1430 values(1001, 'No'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1430 values(1001, 'Yes'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1430 values(1001, 'Yes'); Query OK, 1 row affected (0.16 sec)Display ... Read More

Fetch Date Records Comparing with the Current Date's Day and Month in MySQL

AmitDiwan
Updated on 12-Nov-2019 06:25:00

183 Views

For this, use MONTH() and DAY(). Let us first create a −mysql> create table DemoTable1429    -> (    -> AnniversaryDate date    -> );Insert some records in the table using insert −mysql> insert into DemoTable1429 values('2019-09-29'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1429 values('2018-09-27'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1429 values('2016-09-28'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1429 values('2015-09-29'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select −mysql> select * from DemoTable1429;This will produce the following output −+-----------------+ | AnniversaryDate ... Read More

Count Commas in Every Record from a CSV Column in MySQL

AmitDiwan
Updated on 12-Nov-2019 06:23:46

2K+ Views

Let us first create a −mysql> create table DemoTable1510    -> (    -> Value varchar(50)    -> ); Query OK, 0 rows affected (6.75 sec)Insert some records in the table using insert −mysql> insert into DemoTable1510 values('20, 35'); Query OK, 1 row affected (0.57 sec) mysql> insert into DemoTable1510 values('45, 67, 89'); Query OK, 1 row affected (0.99 sec) mysql> insert into DemoTable1510 values('90, 97, 101, 190'); Query OK, 1 row affected (1.15 sec)Display all records from the table using select −mysql> select * from DemoTable1510;This will produce the following output −+---------------+ | Value         | ... Read More

Filter Column Value by First Character in MySQL

AmitDiwan
Updated on 12-Nov-2019 06:22:16

1K+ Views

You can use LEFT() from MySQL. Let us first create a −mysql> create table DemoTable1428    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(20)    -> ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert −mysql> insert into DemoTable1428(EmployeeName) values('Chris Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1428(EmployeeName) values('Bob Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1428(EmployeeName) values('John Smith'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1428(EmployeeName) values('David Miller'); Query OK, 1 row affected (0.18 sec) ... Read More

Exclude Rows Based on Column Value in MySQL

AmitDiwan
Updated on 12-Nov-2019 06:20:43

528 Views

For this, you can use subquery. Let us first create a −mysql> create table DemoTable1427    -> (    -> StudentId int,    -> StudentMarks int    -> ); Query OK, 0 rows affected (1.28 sec)Insert some records in the table using insert −mysql> insert into DemoTable1427 values(201, 89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1427 values(201, 99); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1427 values(210, 98); Query OK, 1 row affected (0.16 sec)Display all records from the table using select −mysql> select * from DemoTable1427 ;This will produce the following output ... Read More

Simulate the LIMIT MySQL Clause with an Access Database

AmitDiwan
Updated on 12-Nov-2019 06:19:10

707 Views

In Microsoft Access, you can use TOP instead of LIMIT. Let us first create a −Insert some records in the table using insert command −Following is the query to simulate the LIMIT MySQL clause with an Access database −After clicking Run, you will get the desired output −In MySQL, to get top 5 values, you need to use LIMIT 5 −

Order MySQL Records with Fixed Name and Random Display

AmitDiwan
Updated on 12-Nov-2019 05:47:56

122 Views

For this, you can use ORDER BY RAND() with LIMIT. Let us first create a −mysql> create table DemoTable1426    -> (    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert −mysql> insert into DemoTable1426 values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1426 values('Adam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1426 values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1426 values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1426 values('Sam'); Query OK, 1 row ... Read More

Combine DISTINCT and COUNT Queries in MySQL

AmitDiwan
Updated on 12-Nov-2019 05:46:46

187 Views

Yes, you can use aggregate function COUNT(*) along with GROUP BY clause. Let us first create a −mysql> create table DemoTable1425    -> (    -> JoiningYear int    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert −mysql> insert into DemoTable1425 values(2000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1425 values(2010); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1425 values(2015); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1425 values(2000); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1425 values(2010); Query OK, ... Read More

Replace Part of String Before Dot in MySQL Query

AmitDiwan
Updated on 12-Nov-2019 05:44:55

349 Views

For this, use CONCAT() along with SUBSTRING_INDEX(). Let us first create a −mysql> create table DemoTable1424    -> (    -> Value varchar(60)    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert −mysql> insert into DemoTable1424 values('567.78483733'); Query OK, 1 row affected (0.78 sec) mysql> insert into DemoTable1424 values('1023.45252443'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1424 values('7893322.5635543434'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable1424 values('90944.665665'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select −mysql> select * from DemoTable1424;This ... Read More

Fetch Records from Comma Separated Values Using MySQL

AmitDiwan
Updated on 12-Nov-2019 05:42:44

2K+ Views

Use FIND_IN_SET() instead of MySQL IN(). Let us first create a −mysql> create table DemoTable1423    -> (    -> CountryName varchar(100)    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert −mysql> insert into DemoTable1423 values('AUS, UK'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1423 values('US'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1423 values('AUS, UK, US'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select −mysql> select * from DemoTable1423;This will produce the following output −+-------------+ | CountryName | +-------------+ ... Read More

Advertisements