Count Unique Records from a Column in MySQL Database

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

200 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 values('Chris', 'Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('David', 'Hello'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris', 'Awesome'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris', 'Amazing'); Query OK, 1 row affected (0.17 sec) mysql> insert ... Read More

MySQL Query to Display Records Ordered by DESC While Skipping Some

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

91 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 values(1, 'David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(4, 'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(6, 'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(7, 'Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

Find Next Lowest Number Higher Than a Certain Number in MySQL

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

267 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 (0.19 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This ... Read More

Ignore Exceptions Properly in Python

Rajendra Dharmkar
Updated on 27-Sep-2019 07:10:07

589 Views

This can be done by following codestry: x, y =7, 0 z = x/y except: passORtry: x, y =7, 0 z = x/y except Exception: passThese codes bypass the exception in the try statement and ignore the except clause and don’t raise any exception.The difference in the above codes is that the first one will also catch KeyboardInterrupt, SystemExit etc, which are derived directly from exceptions.BaseException, not exceptions.Exception.It is known that the last thrown exception is remembered in Python, some of the objects involved in the exception-throwing statement are kept live until the next exception. We might want to do ... Read More

Display String After Underscore in MySQL

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

126 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 row affected (0.10 sec) mysql> insert into DemoTable values('Smith_David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Smith_Mike'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------------+ | UserName ... Read More

Find Expiry Date Records in MySQL for the Next 2 Days

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

2K+ 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'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-08-20'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values('2018-08-20'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('2019-08-21'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select ... Read More

MySQL Query to Find Average of Rows with the Same ID

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

588 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 values(1001, 88); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(1000, 89); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(1000, 67); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1000, 90); Query OK, 1 row affected (0.21 sec) mysql> insert ... Read More

Create MySQL Boolean Column and Assign Value 1

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

234 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 how you can assign value 1 default to the already created column −mysql> alter table DemoTable CHANGE isAdult isAdult BOOLEAN DEFAULT '1' NOT NULL; Query OK, 0 rows affected (1.18 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable values(); ... Read More

Find Minimum and Maximum Values in a Single MySQL Query

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

400 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 OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement ... Read More

Find Number of Rows in the Last MySQL Query

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

284 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 of such rows −mysql> SELECT SQL_CALC_FOUND_ROWS TABLE_NAME FROM `information_schema`.tables WHERE TABLE_NAME LIKE "DemoTable29%" LIMIT 4;This will produce the following output −+--------------+ | TABLE_NAME | +--------------+ | demotable29 | | demotable290 | | demotable291 | | demotable292 | +--------------+ 4 rows in set (0.01 sec)Here is the query ... Read More

Advertisements