Found 6702 Articles for Database

MySQL Order By specific strings?

Chandu yadav
Updated on 30-Jun-2020 06:33:31

284 Views

Order by the choice of strings you want, using the FIELD() function. The syntax is as follows −SELECT *FROM yourTableName ORDER BY FIELD(yourColumnName, ’yourValue1’, ’yourValue2’, ’yourValue3’, ....N);To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table OrderByListOfStrings    -> (    -> Id int not null auto_increment,    -> CarName varchar(20),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into OrderByListOfStrings(CarName) values('Ford'); Query OK, 1 row affected ... Read More

MySQL concat_ws() method usage

George John
Updated on 30-Jul-2019 22:30:24

100 Views

To get the output MySQL query result in CSV format, use concat_ws(). The syntax is as follows −SELECT CONCAT_WS(‘, ’, yourColumnName1, yourColumnName2, yourColumnName3, ....N) as anyVariableName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table CSVFormatOutputs    -> (    -> StudentId int not null auto_increment,    -> StudentName varchar(20),    -> StudentAge int,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into CSVFormatOutputs(StudentName, StudentAge) ... Read More

How to ORDER BY LIKE in MySQL?

Arjun Thakur
Updated on 30-Jun-2020 06:19:10

2K+ Views

To order by like in MySQL, use the case statement. The syntax is as follows −SELECT *FROM yourTableName    ORDER BY CASE    WHEN yourColumnName like '%yourPatternValue1%' then 1    WHEN yourColumnName like '%yourPatternValue2%' then 2 else 3 end;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table OrderByLikeDemo    -> (    -> Id int,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (1.84 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into OrderByLikeDemo values(100, ... Read More

Find max and second max salary for a MySQL Employee table using subquery?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

173 Views

You can get max and second max salary from an Employee table using subquery.Let us first create a table. The query to create a table is as follows −mysql> create table EmployeeMaxAndSecondMaxSalary    -> (    -> EmployeeId int,    -> Employeename varchar(20),    -> EmployeeSalary int    -> ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command −mysql> insert into EmployeeMaxAndSecondMaxSalary values(1, 'John', 34566); Query OK, 1 row affected (0.20 sec) mysql> insert into EmployeeMaxAndSecondMaxSalary values(2, 'Bob', 56789); Query OK, 1 row affected (0.17 sec) mysql> insert into EmployeeMaxAndSecondMaxSalary values(3, ... Read More

How to output MySQL query results in CSV format and display it on the screen, not a file?

George John
Updated on 30-Jul-2019 22:30:24

518 Views

To get the output MySQL query result in CSV format, use concat(). The syntax is as follows −mysql> select concat(StudentId, ', ', StudentName, ', ', StudentAge) as CSVFormat from CSVFormatOutputs;To understand the above syntax, let us create a table. The query to create a table is as follows−mysql> create table CSVFormatOutputs    -> (    -> StudentId int not null auto_increment,    -> StudentName varchar(20),    -> StudentAge int,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into CSVFormatOutputs(StudentName, ... Read More

Find max and second max salary for a MySQL Employee table?

Ankith Reddy
Updated on 30-Jun-2020 06:20:27

364 Views

You can get max and second max salary from an Employee table using LIMIT OFFSET. The syntax is as follows −SELECT yourColumnName1, yourColumnName2, ....N from yourTableName ORDER BY yourColumnName desc limit 2 offset 0;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table EmployeeMaxAndSecondMaxSalary    -> (    -> EmployeeId int,    -> Employeename varchar(20),    -> EmployeeSalary int    -> ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command −mysql> insert into EmployeeMaxAndSecondMaxSalary values(1, 'John', 34566); Query OK, 1 row ... Read More

How to add column values in MySQL without using aggregate function?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

327 Views

You can add column values without using aggregate function like sum(). For that, the syntax is as follows −SELECT *, (yourColumnName1+yourColumnName2+yourColumnName3, ....N) as anyVariableName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table AddingColumnDemo    -> (    -> StudentId int,    -> StudentName varchar(20),    -> MathMarks int,    -> PhysicsMarks int,    -> ChemistryMarks int    -> ); Query OK, 0 rows affected (0.82 sec)Insert records in the table using insert command. The query is as follows −mysql> insert into AddingColumnDemo values(1, 'John', 35, ... Read More

MySQL String Last Index Of in a URL?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

609 Views

To get the last index, use the SUBSTRING_INDEX() function from MySQL. The syntax is as follows −SELECT yourColumnName1, ...N, SUBSTRING_INDEX(yourColumnName, ’yourDelimiter’, -1)as anyVariableName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table LastIndexString    -> (    -> Id int,    -> yourURL text    -> ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using INSERT command. The query is as follows −mysql> insert into LastIndexString values(1, 'https −//www.example.com/home.html'); Query OK, 1 row affected (0.26 sec) mysql> insert into LastIndexString values(2, ... Read More

What is the data type for unix_timestamp in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

1K+ Views

The best data type for unix_timestamp in MySQL is integer. The integer data type is as followsint(11);The integer data type is useful for condition checking like ( > , create table UnixTime -> ( -> DueTime datetime -> ); Query OK, 0 rows affected (0.55 sec)Insert records in the form of date using insert command. The query is as followsmysql> insert into UnixTime values(now()); Query OK, 1 row affected (0.15 sec) mysql> insert into UnixTime values('2010-10-14'); Query OK, 1 row affected (0.15 sec) mysql> insert into UnixTime values('2020-09-24'); Query ... Read More

How to select yesterday's date in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

505 Views

To select yesterday’s date, use the subdate() function from MySQL. The syntax is as followsselect subdate(yourDatetimeColumnName) as anyVariableName from yourTableName;To understand the above syntax, let us create a tablemysql> create table YesterdayDateDemo -> ( -> VisitedDateTime datetime -> ); Query OK, 0 rows affected (0.59 sec)Let us now insert date in the table using insert command. The query is as followsmysql> insert into YesterdayDateDemo values(now()); Query OK, 1 row affected (0.15 sec) mysql> insert into YesterdayDateDemo values('2012-12-26 13:24:35'); Query OK, 1 row affected (0.17 sec) mysql> insert into YesterdayDateDemo values('2013-10-22 12:20:32'); Query OK, 1 row affected (0.16 sec)Let ... Read More

Advertisements