AmitDiwan has Published 11365 Articles

How can I count unique records from a column in MySQL database?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:17:32

118 Views

For this, use aggregate function count(*) to count to GROUP BY to group. Let us first create a table −mysql> create table DemoTable (    UserName varchar(100),    UserPostMessage text ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable ... Read More

MySQL query to display records ordered by DESC while skipping some?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:14:25

60 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable ... Read More

Find the next lowest number higher than a certain number in MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:11:28

214 Views

For this, use aggregate function MIN() along with WHERE clause. Let us first create a table −mysql> create table DemoTable (    Number int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(11); Query OK, 1 row affected ... Read More

MySQL query to change a string by displaying only the part of string after underscore?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:04:32

66 Views

Let us first create a table −mysql> create table DemoTable (    UserName varchar(100) ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Smith_John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Smith_Adam'); Query OK, 1 ... Read More

MySQL query to find expiry date (record) from the next 2 days?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:01:58

1K+ Views

For this, you can use BETWEEN keyword. Let us first create a table −mysql> create table DemoTable (    ExpiryDate date ); Query OK, 0 rows affected (0.55 sec)Note − Let’s say the current date is 2019-08-18.Insert some records in the table using insert command −mysql> insert into DemoTable values('2018-01-21'); ... Read More

MySQL query to find the average of rows with the same ID

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 06:58:20

451 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentMarks int ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1000, 78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable ... Read More

How can I create a MySQL boolean column and assign value 1 while altering the same column?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 06:55:48

162 Views

To assign value 1 while altering, use the MySQL DEFAULT. This will itself enter 1 if nothing is inserted in the same column while using the INSERT command.Let us first create a table −mysql> create table DemoTable (    isAdult int ); Query OK, 0 rows affected (1.39 sec)Following is ... Read More

How to find the minimum and maximum values in a single MySQL Query?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 06:53:29

305 Views

To find the minimum and maximum values in a single query, use MySQL UNION. Let us first create a table −mysql> create table DemoTable (    Price int ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88); Query ... Read More

MySQL query to find the number of rows in the last query

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 06:51:16

161 Views

For this, use the FOUND_ROWS in MySQL. Following is the syntax −SELECT SQL_CALC_FOUND_ROWS TABLE_NAME FROM `information_schema`.tables WHERE TABLE_NAME LIKE "yourValue%" LIMIT yourLimitValue;Here, I am using the database ‘web’ and I have lots of tables, let’s say which begins from DemoTable29. Let us implement the above syntax to fetch only 4 ... Read More

Get only the date from datetime in MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 06:49:22

2K+ Views

To get only the date from DateTime, use the date format specifiers −%d for day %m for month %Y for yearLet us first create a table −mysql> create table DemoTable (    AdmissionDate datetime ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command ... Read More

Advertisements