AmitDiwan has Published 10740 Articles

MySQL query to count rows with a specific column?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 10:55:38

278 Views

Let us first create a table −mysql> create table DemoTable841(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value varchar(100) ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable841(Value) values('X'); Query OK, 1 row affected (0.19 sec) mysql> ... Read More

Implement MySQL IN for 2 columns to display only selected records

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 10:53:53

114 Views

Let us first create a table −mysql> create table DemoTable810(    First int,    Second int ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable810 values(20, 40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable810 values(70, ... Read More

MySQL SELECT to sum a column value with previous value

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 10:51:46

906 Views

For this, you can use a session variable. Let us first create a table −mysql> create table DemoTable809(Price int); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable809 values(40); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable809 ... Read More

Implement specific record ordering with MySQL

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 10:48:49

129 Views

To set specific record ordering, use ORDER BY LIKE. Let us first create a table−mysql> create table DemoTable808(Value varchar(100)); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable808 values('smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable808 ... Read More

Check if a value exists in a column in a MySQL table?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 10:46:28

4K+ Views

Let us first create a table −mysql> create table DemoTable807(    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(100),    ClientCountryName varchar(100) ); Query OK, 0 rows affected (0.64 sec) Insert some records in the table using insert command −mysql> insert into DemoTable807(ClientName, ClientCountryName) values('Chris', 'UK'); Query ... Read More

Is there a way to select a value which matches partially in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 10:42:31

236 Views

To match partially, use LIKE operator. Let us first create a table −mysql> create table DemoTable806(    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100),    StudentSubject varchar(100) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable806(StudentName, ... Read More

How to split the datetime column into date and time and compare individually in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 10:39:11

3K+ Views

Let us first create a table −mysql> create table DemoTable805(LoginDate datetime); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable805 values('2019-01-31 12:45:20'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable805 values('2017-11-01 10:20:30'); Query OK, 1 row affected ... Read More

Match column values on the basis of the other two column values in MySQL

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 08:59:52

169 Views

Let us first create a table −mysql> create table DemoTable774 ( Id int, FirstName varchar(100), MatchId int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable774 values(101, 'Chris', 104); Query ... Read More

MySQL query to return 5 random records from last 20 records?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 08:57:54

447 Views

For this, you need to use ORDER BY to order records. With that use RAND() to get random records and LIMIT 5 since we want to display only 5 random records.Let us first create a table −mysql> create table DemoTable773 (StudentId int); Query OK, 0 rows affected (0.59 sec)Insert some ... Read More

MySQL query to find alternative records from a table

AmitDiwan

AmitDiwan

Updated on 03-Sep-2019 08:54:45

387 Views

To find alternative records from a table, you need to use the OR condition as in the below syntax −select *from yourTableName where yourColumnName=yourValue1 OR yourColumnName=yourValue2…...N;Let us first create a table −mysql> create table DemoTable772 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name ... Read More

Advertisements