Found 4378 Articles for MySQL

How to use a single MySQL query to count column values ignoring null?

karthikeya Boyini
Updated on 30-Jun-2020 09:57:40

113 Views

For this, you can COUNT() method, which does not include NULL value. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100),    -> CountryName varchar(100)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', null); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris', 'US'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Robert', null); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Bob', 'UK'); Query ... Read More

How to sort an alphanumeric column in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 09:58:33

560 Views

To sort an alphanumeric column, use LIKE operator along with SUBSTRING(). Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (1.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('S/TU/100'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('S/TU/1000'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('S/TU/10'); Query OK, 1 row affected (0.47 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following ... Read More

How to filter a specific month in MySQL when date is in varchar?

karthikeya Boyini
Updated on 30-Jun-2020 09:59:50

343 Views

To filter, you can use STR_TO_DATE() function from MySQL. With that, use MONTH() to get the date from the specific month. Let us first create a table −mysql> create table DemoTable    -> (   -> DueDate varchar(100)    -> ); Query OK, 0 rows affected (1.18 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('06-19-2019'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('01-31-2018'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values('12-01-2016'); Query OK, 1 row affected (0.14 sec)Display all records from the table using ... Read More

MySQL query to convert timediff() to seconds?

Sharon Christine
Updated on 30-Jun-2020 10:01:16

440 Views

For this, you can use TIME_TO_SEC() function. Let us first create a table −mysql> create table DemoTable    -> (    -> SourceTime time,    -> DestinationTime time    -> ); Query OK, 0 rows affected (1.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10:20:00', '4:50:54'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('12:05:10', '7:45:12'); Query OK, 1 row affected (0.30 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------------+-----------------+ | SourceTime | DestinationTime | +------------+-----------------+ | 10:20:00   ... Read More

MySQL query to get the count of distinct records in a column

Sharon Christine
Updated on 30-Jun-2020 10:04:28

224 Views

To get the count of distinct records, use DISTINCT along with COUNT(). Following is the syntax −select count(DISTINCT yourColumnName) from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(20), -> Score int -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 56); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Sam', 89); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('John', 56); Query OK, 1 row affected (0.17 sec) mysql> insert into ... Read More

Find out the records of students with more than a specific score in MySQL?

Sharon Christine
Updated on 30-Jun-2020 10:07:06

98 Views

Set it with WHERE and get the records of students more than a specific score. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100),    -> StudentScore int    -> ); Query OK, 0 rows affected (1.65 secInsert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentScore) values('John', 43); Query OK, 1 row affected (0.81 sec) mysql> insert into DemoTable(StudentName, StudentScore) values('Sam', 48); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName, StudentScore) values('Chris', 33); ... Read More

MySQL query to find all rows where string contains less than four characters?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

269 Views

Use CHAR_LENGTH() and find the count of characters in every string and then get the strings which are less than four characters. Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (1.38 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (1.60 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.25 ... Read More

Implement MySQL ORDER BY without using ASC or DESC?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

98 Views

For this, you can use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (2.25 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.91 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.69 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.72 sec) mysql> insert into DemoTable values(800); Query OK, 1 row affected (0.75 sec)Display all records ... Read More

Best data type for storing large strings in MySQL?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

746 Views

You can use text data type to store large strings. Following is the syntax −CREATE TABLE yourTableName (    yourColumnName text,    .    .    N );Let us first create a table −mysql> create table DemoTable -> ( -> MyStringValue text -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('This is a text data type to store large string'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from ... Read More

How to extract the area codes from a phone number with MySQL?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

896 Views

Let’s say we have a list of phone numbers and from that we want to get the area codes. These area codes are for example, the first 3 digits of the phone number. Use LEFT() function from MySQL for this.Let us first create a table −mysql> create table DemoTable -> ( -> AreaCodes varchar(100) -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command. Here, let’s say we have included the phone numbers −mysql> insert into DemoTable values('90387568976') ; Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('90389097878' ; ... Read More

Advertisements