Found 4381 Articles for MySQL

How to sort MySQL output on the basis of the column which is not in the result set?

Lakshmi Srinivas
Updated on 30-Jan-2020 05:18:16

170 Views

It is quite possible to get the sorted output on the basis of the column which is not even the part of that output or not in the result set. It can be done by selecting the required fields and writing the name of the fields on the basis of which sorting order is desired. Following is an example to demonstrate it, in which we sorted out the result set, having ‘Name’ and ‘Address’ fields, on the basis of column ‘id’.mysql> Select Name, Subject From Student ORDER BY Id; +---------+-----------+ | Name    | Subject   | +---------+-----------+ | Gaurav ... Read More

What MySQL returns if I use enclosed set of unit values with INTERVAL keyword?

Vrundesha Joshi
Updated on 30-Jan-2020 05:19:14

112 Views

In this case, MySQL will take into consideration the first value out of two provided in the enclosed set of unit values. It will return the output along with warning after calculating the interval, based on the considered value from an enclosed set, on the unit given in INTERVAL keyword. The following example will clarify it −mysql> Select TIMESTAMP('2017-10-22 04:05:36' + INTERVAL '4 2' Hour)AS 'HOUR VALUE INCREASED BY 4'; +---------------------------+ | HOUR VALUE INCREASED BY 4 | +---------------------------+ | 2017-10-22 08:05:36       | +---------------------------+ 1 row in set, 1 warning (0.00 sec) mysql> Show warnings; +---------+------+------------------------------------------+ | ... Read More

What MySQL returns if I write only one value in the enclosed set of unit values for compound INTERVAL unit?

mkotla
Updated on 30-Jan-2020 05:20:18

113 Views

In this case, MySQL will take into consideration right most unit given in compound INTERVAL unit. It will return the output after calculating the interval on the basis of single value provided in the enclosed set of unit values. The following example will clarify it −mysql> Select TIMESTAMP('2017-10-22 04:05:36' + INTERVAL '2 ' year_month) AS 'Only Month Value Changed'; +--------------------------+ | Only Month Value Changed | +--------------------------+ | 2017-12-22 04:05:36      | +--------------------------+ 1 row in set (0.00 sec)The query above changes the month (right most in compound INTERVAL unit) from 10 to 12 based on the single value ... Read More

How can we sort MySQL output in ascending order?

karthikeya Boyini
Updated on 20-Jun-2020 06:26:13

285 Views

We need to specify ASC (short form for ASCENDING) keyword in ORDER BY clause if we want to sort out the result set in ascending order.SyntaxSelect column1, column2,…,columN From table_name ORDER BY column1[column2,…] ASC;ExampleIn the following example, we have sorted the result set by column ‘Name’ in the ascending order.mysql> Select * from Student ORDER BY Name ASC; +------+---------+---------+-----------+ | Id   | Name    | Address | Subject   | +------+---------+---------+-----------+ | 2    | Aarav   | Mumbai  | History   | | 1    | Gaurav  | Delhi   | Computers | | 15   | Harshit | Delhi   | Commerce  | | 17   | Raman   | Shimla  | Computers | +------+---------+---------+-----------+ 4 rows in set (0.00 sec)

How to use compound INTERVAL unit in MySQL?

Rishi Rathor
Updated on 20-Jun-2020 06:26:43

167 Views

Compound INTERVAL unit keywords are made up of two keywords and separated by an underscore (_). For using them in MySQL the unit values must be enclosed in single quotes and separated by space.Example − Following query will add 2 years and 2 months in the date value.mysql> Select timestamp('2017-10-22 04:05:36' + INTERVAL '2 2' year_month) AS'Date After 2 Years and 2 Months'; +---------------------------------+ | Date After 2 Years and 2 Months | +---------------------------------+ | 2019-12-22 04:05:36             | +---------------------------------+ 1 row in set (0.00 sec)

What is the range of date time value that we can pass as an argument to MySQL UNIX_TIMESTAMP function?

Giri Raju
Updated on 30-Jan-2020 05:24:36

191 Views

The range of date time value that we can pass as an argument to MySQL UNIX_TIMESTAMP function is the same as the range of TIMESTAMP data type i.e. between ‘1970-01-01 00:00:01’ to ‘2038-01-19 08:44:07’. If we give the date time values in UNIX_TIMESTAMP function beyond or below TIMESTAMP range, MySQL will return 0 as output. It can be understood with the help of the following example −mysql> select UNIX_TIMESTAMP('2038-01-19 08:44:07'); +---------------------------------------+ | UNIX_TIMESTAMP('2038-01-19 08:44:07') | +---------------------------------------+ | 2147483647                            | +---------------------------------------+ 1 row in set (0.00 sec) ... Read More

How can we sort MySQL output in descending order?

Rishi Raj
Updated on 20-Jun-2020 06:25:46

212 Views

We need to specify DESC (short form for DESCENDING) keyword in ORDER BY clause if we want to sort out the result set in descending order.SyntaxSelect column1, column2,…,columN From table_name ORDER BY column1[column2,…] DESC;ExampleIn the following example, we have sorted the result set by column ‘Id’ in the descending order.mysql> Select * from Student ORDER BY Id DESC; +------+---------+---------+-----------+ | Id   | Name    | Address | Subject   | +------+---------+---------+-----------+ | 17   | Raman   | Shimla  | Computers | | 15   | Harshit | Delhi   | Commerce  | | 2    | Aarav   | Mumbai  | History   | | 1    | Gaurav  | Delhi   | Computers | +------+---------+---------+-----------+ 4 rows in set (0.00 sec)

How can we get sorted output based on multiple columns?

Ankith Reddy
Updated on 20-Jun-2020 06:25:11

213 Views

We can specify multiple columns in ORDER BY clause to get the sorted output based on those multiple columns. Following as an example to make this concept clearer −mysql> Select * from Student ORDER BY Name, Address; +------+---------+---------+-----------+ | Id   | Name    | Address | Subject   | +------+---------+---------+-----------+ | 2    | Aarav   | Mumbai  | History   | | 1    | Gaurav  | Delhi   | Computers | | 15   | Harshit | Delhi   | Commerce  | | 17   | Raman   | Shimla  | Computers | +------+---------+---------+-----------+ 4 rows in set (0.12 sec)

How can I use the arithmetic operators (+,-,*,/) with unit values of INTERVAL keyword in MySQL?

Nancy Den
Updated on 20-Jun-2020 06:24:28

209 Views

We can use arithmetic operators (+, -, *, /) with the unit values of INTERVAL keyword as follows −Use of Addition (+)mysql> Select date('2017-10-22' + INTERVAL 2+2 Year) AS 'Date After (2+2)Years'; +------------------------+ | Date After (2+2) Years | +------------------------+ | 2021-10-22             | +------------------------+ 1 row in set (0.00 sec)Use of Subtraction (-)mysql> Select date('2017-10-22' + INTERVAL 2-2 Year) AS 'Date After (2-2)Years'; +------------------------+ | Date After (2-2) Years | +------------------------+ | 2017-10-22             | +------------------------+ 1 row in set (0.00 sec)Use of Multiplication (*)mysql> Select date('2017-10-22' + INTERVAL ... Read More

How many digits should be there in string or number so that it can be specified as a date value by MySQL?

Sreemaha
Updated on 29-Jan-2020 06:43:57

131 Views

While considering the year as 4-digit value, minimum of 8 digits in a string or number is required for MySQL to specify it as a date value. In this case, if we also want to store microseconds then the value can be up to a maximum of 20 digits.mysql> Select TIMESTAMP('20171022040536.100000'); +-----------------------------------+ | TIMESTAMP('20171022040536100000') | +-----------------------------------+ | 2017-10-22 04:05:36.100000        | +-----------------------------------+ 1 row in set, 1 warning (0.00 sec)The query above is taking 20 digits string for TIMESTAMP value. Last 6 digits are for microseconds.mysql> Select TIMESTAMP(20171022); +---------------------+ | TIMESTAMP(20171022) | +---------------------+ | 2017-10-22 00:00:00 | +---------------------+ ... Read More

Advertisements