Found 6702 Articles for Database

Count number of occurrences of records in a MySQL table and display the result in a new column?

AmitDiwan
Updated on 31-Dec-2019 06:26:13

2K+ Views

For this, use COUNT(*) along with GROUP BY clause. Let us first create a table −mysql> create table DemoTable1942    (    Value int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1942 values(1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(2); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(3); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(2); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(3); Query OK, 1 row affected (0.00 ... Read More

Return the field with highest count in MySQL

AmitDiwan
Updated on 30-Dec-2019 08:02:13

1K+ Views

To return the field with highest count, use ORDER BY COUNT(*). Let us first create a table −mysql> create table DemoTable1940    (    FirstName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1940 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Adam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Chris'); Query OK, 1 row ... Read More

MySQL query to select everything to left of last space in a column with name records

AmitDiwan
Updated on 30-Dec-2019 07:58:21

154 Views

For this, you can use LEFT(). Let us first create a table −mysql> create table DemoTable1939    (    FullName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1939 values('Adam Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1939 values('Robert Downey, Jr.'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1939 values('Sylvester Stallone'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1939 values('Chris Hemsworth'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> ... Read More

Display all fields of a table in MySQL?

AmitDiwan
Updated on 30-Dec-2019 07:56:20

1K+ Views

To display all fields, set the database with table_schema and specific table with table_name as in the below syntax −select column_name as anyAliasName from information_schema.columns    where table_schema=database()    and table_name=’yourTableName’\GLet us first create a table −mysql> create table DemoTable1938    (    StudentId int,    StudentName varchar(20),    StudentAge int,    StudentCountryName varchar(20),    StudentMobileNumber bigint    ); Query OK, 0 rows affected (0.00 sec)Here is the query to display all fields of a table −mysql> select column_name as ALL_FIELDS from information_schema.columns    where table_schema=database()    and table_name='DemoTable1938'\GThis will produce the following output −*************************** 1. row *************************** ALL_FIELDS: StudentId ... Read More

MySQL ORDER BY ASC and display NULLs at the bottom?

AmitDiwan
Updated on 30-Dec-2019 07:54:41

416 Views

For this, use CASE statement with ORDER BY. Let us first create a table −mysql> create table DemoTable1937    (    Name varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1937 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1937 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1937 values('Adam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1937 values('John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1937 values(''); Query OK, 1 row affected (0.00 sec) ... Read More

Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL

AmitDiwan
Updated on 30-Dec-2019 07:51:53

2K+ Views

To perform multiple inserts, the syntax is as follows −insert into yourTableName(yourColumnName1, yourColumnName2, yourColumnName3, ..N)    select yourValue1 as yourColumnName1, yourValue2 as yourColumnName2, yourValue3 as yourColumnName3, ......N    union    select yourValue1 as yourColumnName1, yourValue2 as yourColumnName2, yourValue3 as yourColumnName3, ......N . . NTo understand the above syntax, let us create a table −mysql> create table DemoTable1936    (    StudentId int,    StudentName varchar(20),    StudentCountryName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1936(StudentId, StudentName, StudentCountryName)    select 1001 as StudentId, 'Chris' as StudentName, 'US' ... Read More

MySQL query to display records from a table filtered using LIKE with multiple words?

AmitDiwan
Updated on 30-Dec-2019 07:49:12

193 Views

For this, use RLIKE and filter records as in the below syntax &Minus;select * from yourTableName    where yourColumnName rlike 'yourValue1|yourValue2';Let us first create a table −mysql> create table DemoTable1935    (    Subject varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1935 values('MySQL'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1935 values('Python'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1935 values('MongoDB'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1935 values('SQL Server'); Query OK, 1 row affected (0.00 ... Read More

MySQL query to select average from distinct column of table?

AmitDiwan
Updated on 30-Dec-2019 07:46:21

458 Views

For getting average, use AVG() and use it with DISTINCT to calculate from distinct records. Let us first create a table −mysql> create table DemoTable1934    (    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1934 values('Chris', 56); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1934 values('Chris', 56); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1934 values('David', 78); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1934 values('David', 78); Query OK, 1 row affected ... Read More

Display table records from a stored procedure in MySQL

AmitDiwan
Updated on 30-Dec-2019 07:43:55

1K+ Views

Let us first create a table −mysql> create table DemoTable1933    (    ClientName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1933 values('Chris Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1933 values('David Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1933 values('Adam Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1933 values('John Doe'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1933;This will produce the ... Read More

Select items based on value first, then order on the basis of date for rest of the records in MySQL

AmitDiwan
Updated on 30-Dec-2019 07:42:25

290 Views

Use ORDER BY to fix a record and then displayselect * from yourTableName order by yourColumnName1=yourValue desc, yourColumnName2;Let us first create a table −mysql> create table DemoTable1932    (    UserName varchar(20),    ShippingDate date    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1932 values('Chris', '2018-10-12'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1932 values('David', '2019-04-11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1932 values('Mike', '2016-12-04'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1932 values('Carol', '2017-12-26'); Query OK, 1 ... Read More

Advertisements