Found 4378 Articles for MySQL

Get the count of unique phone numbers from a column with phone numbers declared as BIGINT type in MySQL

AmitDiwan
Updated on 03-Oct-2019 06:04:24

636 Views

For this, you can use COUNT() along with DISTINCT. The COUNT() method is to count the records. However, the DISTINCT returns distinct records, whereas COUNT() method counts those unique records. Let us first create a table −mysql> create table DemoTable (    PhoneNumber bigint ); Query OK, 0 rows affected (1.29 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(8567789898); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(8567789898); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values(9876564534); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable ... Read More

Only display row with highest ID in MySQL

AmitDiwan
Updated on 03-Oct-2019 06:02:55

116 Views

To order, use the ORDER BY DESC clause. With that, since we want a single ID, which should be the highest, use LIMIT 1. This will fetch the row with highest ID. Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(50) ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(110, 'Robert'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(120, 'Mike'); Query OK, 1 ... Read More

MySQL CREATE statement with KEY keyword

AmitDiwan
Updated on 01-Oct-2019 08:56:32

116 Views

As stated in the official docs −KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(50),    Age int ); Query OK, 0 rows affected (0.69 sec)Following is the query for INDEX, which is a synonym to KEY −mysql> create index Name_Age_Index on DemoTable(Name, Age); Query OK, 0 rows affected (0.65 sec) Records: 0 Duplicates: ... Read More

MySQL query to select a specific string with special characters

AmitDiwan
Updated on 01-Oct-2019 08:53:51

601 Views

Let us first create a table −mysql> create table DemoTable (    Title text ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('MongoDB\'s'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('MySQL\'s'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Java\'s'); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | Title     ... Read More

How to perform conditional GROUP BY in MySQL to fetch?

AmitDiwan
Updated on 01-Oct-2019 08:52:18

253 Views

Let us first create a table −mysql> create table DemoTable (    StudentName varchar(40),    StudentMarks int ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 78); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris', 48); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('John', 67); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Chris', 89); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

Display two different columns from two different tables with ORDER BY?

AmitDiwan
Updated on 01-Oct-2019 08:50:21

560 Views

For this, you can use UNION along with the ORDER BY clause. Let us first create a table −mysql> create table DemoTable1 (    Amount int ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(234); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1 values(567); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(134); Query OK, 1 row affected (0.43 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+--------+ | Amount | +--------+ ... Read More

Using MySQL keywords in a query surrounded with single quotes?

AmitDiwan
Updated on 01-Oct-2019 08:47:54

102 Views

If there are multiple MySQL keywords in a query, use backticks symbol rather than single quotes. Let us first create a table. Here, we have used two reserved keywords i.e. ‘key’ and ‘Limit’ −mysql> create table DemoTable (    `key` int NOT NULL AUTO_INCREMENT PRIMARY KEY ,    `Limit` int ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(`key`, `Limit`) values(null, 80); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable(`key`, `Limit`) values(null, 90); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(`key`, `Limit`) values(null, ... Read More

MySQL SELECT to skip first N results?

AmitDiwan
Updated on 01-Oct-2019 08:46:17

226 Views

To skip records in MySQL SELECT, use OFFSET. Let us first create a table−mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.11 sec)Display all records from ... Read More

How to speed up SELECT DISTINCT in MySQL

AmitDiwan
Updated on 01-Oct-2019 08:44:43

574 Views

To speed up SELECT DISTINCT, you can create an index on the column or set of columns. Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (1.13 sec)Following is the query to create an index −mysql> create index Name_Index on DemoTable(Name); Query OK, 0 rows affected (1.56 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.15 sec) mysql> insert into ... Read More

MySQL query to calculate the total amount from column values with Cost and Quantity?

AmitDiwan
Updated on 01-Oct-2019 08:37:25

841 Views

Let us first create a table −mysql> create table DemoTable (    Cost int,    Quantity int ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(65, 2); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(290, 4); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(40, 3); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------+----------+ | Cost | Quantity | +------+----------+ | 65 ... Read More

Advertisements