AmitDiwan has Published 10744 Articles

Fetch random rows from a table with MySQL

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:47:30

175 Views

For this, you can use a PREPARE statement. Let us first create a table −mysql> create table DemoTable(    FirstName varchar(100),    CountryName varchar(100) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 'US'); Query OK, 1 row ... Read More

How to find repeated rows and display there count in a separate column with MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:45:57

135 Views

For this, use the GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable(    Name varchar(100),    Age int ); Query OK, 0 rows affected (1.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 23); Query OK, 1 row ... Read More

Select with set order in MySQL

AmitDiwan

AmitDiwan

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

165 Views

For this, you need to use IN() and after that FIELD() method. Let us first create a table −mysql> create table DemoTable(    StudentId varchar(10),    StudentName varchar(20) ) ; Query OK, 0 rows affected (4.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10001', ... Read More

How to search a column for an exact string in MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:42:41

2K+ Views

For exact string, you need to use wildcard ‘%’ with LIKE operator −select *from yourTableName where binary yourColumnName LIKE '%yourStringValue%';Let us first create a table −mysql> create table DemoTable(    Name varchar(20) ); Query OK, 0 rows affected (1.93 sec)Insert some records in the table using insert command −mysql> insert ... Read More

Count zero, NULL and distinct values except zero and NULL with a single MySQL query

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:41:21

536 Views

Let us first create a table −mysql> create table DemoTable(    Value int ); Query OK, 0 rows affected (1.35 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row ... Read More

Is SELECT * faster than 40 columns listing in MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:39:55

139 Views

The SELECT * is slower than 40 columns listing. It’s a better choice to list the column names while using the SELECT query. Let us see a simple example and create a table −mysql> create table DemoTable(    Id int,    Name varchar(20),    Age int,    ZipCode varchar(20),   ... Read More

Get second largest marks from a MySQL table using subquery?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:37:46

326 Views

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

MySQL query to count number of duplicate values in a table column

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:36:12

219 Views

Let us first create a table −mysql> create table DemoTable(    Data int ); Query OK, 0 rows affected (0.98 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(40); Query OK, 1 row ... Read More

A single MySQL query to select value from first table and insert in the second?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:34:35

208 Views

Let us first create a table −mysql> create table DemoTable1(    Value int ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(67); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1 values(46); Query OK, 1 row ... Read More

MySQL ENUM column match for quoted values

AmitDiwan

AmitDiwan

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

122 Views

Let us first create a table with ENUM type column −mysql> create table DemoTable(    isMarried ENUM('1', '0') ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('0'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable ... Read More

Advertisements