Order By Grouped Fields in MySQL

AmitDiwan
Updated on 07-Oct-2019 12:09:11

131 Views

To ORDER BY grouped fields, use ORDER BY CASE along with IN(). CASE evaluates different conditions whereas ORDER BY sort values in ascending or descending order. The MySQL IN() is used to find a match.Let us first create a table −mysql> create table DemoTable (    Value varchar(40) ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('100&101'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.09 sec) ... Read More

Check If Value Exists with MySQL SELECT

AmitDiwan
Updated on 07-Oct-2019 12:06:47

376 Views

Use SELECT 1 for this as in the below syntax −select 1 from yourTableName where yourColumnName=yourValue;If the above returns 1, that means value exists in the MySQL database. Let us first see an example and create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(40),    StudentAge int ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentAge) values('Chris', 21); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName, StudentAge) values('David', 20); Query OK, 1 row affected (0.16 ... Read More

MySQL CASE Statement to Replace NULL Values

AmitDiwan
Updated on 07-Oct-2019 12:04:05

191 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(20) ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | FirstName | +-----------+ ... Read More

MySQL Query to Return Multiple Row Records with AND and OR Operator

AmitDiwan
Updated on 07-Oct-2019 11:55:50

458 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(40),    StudentMathMarks int,    StudentMySQLMarks int,    status ENUM('ACTIVE', 'INACTIVE') ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentMathMarks, StudentMySQLMarks, status) values('Chris', 45, 67, 'active'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentName, StudentMathMarks, StudentMySQLMarks, status) values('Bob', 89, 78, 'inactive'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentName, StudentMathMarks, StudentMySQLMarks, status) values('David', 56, 68, 'active'); Query OK, 1 row affected ... Read More

Difference Between BIGINT and BIGINT(20) in MySQL

AmitDiwan
Updated on 07-Oct-2019 11:49:55

4K+ Views

The only difference between BIGINT and BIGINT(20) is for displaying width. The 20 can be used for displaying width.Let us see an example and create a table. Here, we have set BIGINT(20) −mysql> create table DemoTable (    Number bigint(20) zerofill ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(123); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1234); Query OK, ... Read More

Use MySQL LIKE Clause to Fetch Multiple Values Beginning with 'Joh'

AmitDiwan
Updated on 07-Oct-2019 11:47:15

204 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Ethan'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Johnson'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected ... Read More

Get Left Part of String Based on Last Occurrence of Delimiter in MySQL

AmitDiwan
Updated on 07-Oct-2019 11:42:27

169 Views

For this, use LEFT() method. For manipulation, we have used the LOCATE() and the REVERSE() method.Let us first create a table −mysql> create table DemoTable (    Title text ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('$/This$is[MySQL]$/MySQL[FirstClass]$MySQL[SecondClass]'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('$/This$is[Java]$/Java[FirstClass]$Java[SecondClass]'); 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 −+--------------------------------------------------------+ | Title                       ... Read More

Count Sub-Part of Duplicate Values in IP Address Records

AmitDiwan
Updated on 07-Oct-2019 11:40:02

156 Views

For such string manipulations, you need to use MySQL SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable (    SystemIpAddress text ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('192.168.130.67'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('192.168.130.87'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('192.168.131.47'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values('192.168.134.50'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('192.168.131.12'); Query OK, 1 row affected (0.21 sec)Display ... Read More

Insert Auto Increment Column Values in MySQL

AmitDiwan
Updated on 07-Oct-2019 11:34:56

151 Views

No, there’s no need to insert auto_increment column values, since it begins from 1 and inserts on its own. This is because we have set it as auto increment. Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT,    EmployeeName varchar(30),    EmployeeSalary int,    PRIMARY KEY(EmployeeId) ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName, EmployeeSalary) values('Chris', 56789); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(EmployeeName, EmployeeSalary) values('David', 78909); Query OK, 1 row affected (0.14 sec) mysql> ... Read More

Replace Column Value in MySQL Query

AmitDiwan
Updated on 07-Oct-2019 11:30:23

406 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score int ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Score) values(56); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(Score) values(78); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Score) values(34); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Score) values(55); Query OK, 1 row affected (0.37 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce ... Read More

Advertisements