Found 4381 Articles for MySQL

How can we use group functions with non-group fields in MySQL SELECT query?

Swarali Sree
Updated on 20-Jun-2020 06:33:44

237 Views

We have to use GROUP BY clause if we want to use group functions with non-group fields in the SELECT query. The general syntax can be as followsSyntaxSELECT group_function1, …, non-group-column1, … from table_name GROUP BY column_name;Examplemysql> Select COUNT(*), id from Student GROUP BY id; +----------+------+ | COUNT(*) | id   | +----------+------+ | 1        | 1    | | 1        | 2    | | 1        | 15   | | 1        | 17   | | 1        | 20   | +----------+------+ 5 ... Read More

What is the difference between UNIX TIMESTAMPS and MySQL TIMESTAMPS?

vanithasree
Updated on 20-Jun-2020 06:29:55

2K+ Views

In MySQL, UNIX TIMESTAMPS are stored as 32-bit integers. On the other hand MySQL TIMESTAMPS are also stored in similar manner but represented in readable YYYY-MM-DD HH:MM:SS format.Examplemysql> Select UNIX_TIMESTAMP('2017-09-25 02:05:45') AS 'UNIXTIMESTAMP VALUE'; +---------------------+ | UNIXTIMESTAMP VALUE | +---------------------+ | 1506285345          | +---------------------+ 1 row in set (0.00 sec)The query above shows that UNIX TIMESTAMPS values are stored as 32 bit integers whose range is same as MySQL INTEGER data type range.mysql> Select FROM_UNIXTIME(1506283345) AS 'MySQLTIMESTAMP VALUE'; +----------------------+ | MySQLTIMESTAMP VALUE | +----------------------+ | 2017-09-25 01:32:25  | +----------------------+ 1 row in set (0.00 sec)The query ... Read More

How Groups function can be used in MySQL SELECT clause?

Alankritha Ammu
Updated on 20-Jun-2020 06:29:28

150 Views

As we know that group function operates on the sets of value that is why if group functions will be used in SELECT clause then they will be used on the rows that meet the query selection criteria and the output of group functions will be returned as the output of the query.ExampleIn the example below, we have used some group functions in a SELECT statement on the fields of ‘Student’ table and the output of the statement is the output of those group functions −mysql> Select COUNT(Name), MIN(Id), AVG(Id), MAX(Id), COUNT(*) from Student; +-------------+---------+---------+---------+----------+ | COUNT(Name) | MIN(Id) | ... Read More

What happens if the output of MySQL TIMEDIFF() function surpass the range value of TIME field?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

243 Views

As we know that the range of TIME field in MySQL is ‘-838:59:59’ to ‘838:59:59’. Now, if TIMEDIFF() function’s output surpasses this range then MySQL will return either ‘-838:59:59’ or ‘838:59:59’ depends upon the values of the argument. Example mysql> Select TIMEDIFF('2017-09-01 03:05:45', '2017-10-22 03:05:45')AS 'Out of Range TIME Difference'; +------------------------------+ | Out of Range TIME Difference | +------------------------------+ | -838:59:59 | +------------------------------+ 1 row in set, 1 warning (0.00 sec) mysql> Select TIMEDIFF('2017-10-22 04:05:45', '2017-09-01 03:05:45')AS 'Out of Range ... Read More

What MySQL returns if we include date components along with time component as an argument to TIMEDIFF() function?

radhakrishna
Updated on 20-Jun-2020 06:28:42

118 Views

MySQL would return the output in time value after converting the difference between date-and-time values being provided to TIMEDIFF() function as arguments.Example mysql> Select TIMEDIFF('2017-10-22 04:05:45', '2017-10-21 03:04:44')AS 'Difference in Time'; +--------------------+ | Difference in Time | +--------------------+ | 25:01:01           | +--------------------+ 1 row in set (0.00 sec)Here in this example, we can see that MySQL converts the difference in date-and-time values into time values and return the output in time value as well.

How can we calculate the difference between two time values in MySQL?

Abhinaya
Updated on 20-Jun-2020 06:28:18

254 Views

With the help of TIMEDIFF() MySQL function the difference between two-time values can be calculated.Examplemysql> Select TIMEDIFF('04:05:45','03:05:45') AS ‘Difference in Time’; +---------------------------------+ | Difference in Time              | +---------------------------------+ | 01:00:00                        | +---------------------------------+ 1 row in set (0.00 sec)

How can I use 2-digit year value in MySQL DATEDIFF() function?

Govinda Sai
Updated on 30-Jan-2020 05:34:10

276 Views

We can use 2-digit year value either in single date expression or in both date expressions used as argument/s in MySQL DATEDIFF() function.For example, the query below is using 2-digit year value in first date expression and other is having 4-digit year value.mysql> Select DATEDIFF('18-10-22', '2017-10-22'); +-----------------------------------+ | DATEDIFF('18-10-22', '2017-10-22') | +-----------------------------------+ |                               365 | +-----------------------------------+ 1 row in set (0.00 sec)And the query below is using 2-digit year value in both date expressions.mysql> Select DATEDIFF('18-10-22', '17-10-22'); +---------------------------------+ | DATEDIFF('18-10-22', '17-10-22') | +---------------------------------+ |   ... Read More

How is it possible to filter out the duplications in the rows of result set return by MySQL?

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

110 Views

It can be possible by using the DISTINCT keyword in SELECT clause. The DISTINCT applies to the combination of all data fields specified in SELECT clause.ExampleWe have the table ‘Student’ on which we have applied DISTINCT keyword as follows −mysql> Select * from student; +------+---------+---------+-----------+ | Id   | Name    | Address | Subject   | +------+---------+---------+-----------+ | 1    | Gaurav  | Delhi   | Computers | | 2    | Aarav   | Mumbai  | History   | | 15   | Harshit | Delhi   | Commerce  | | 17   | Raman   | ... Read More

How can we use MySQL SELECT statement to count number of rows in a table?

Paul Richard
Updated on 20-Jun-2020 06:29:06

240 Views

We need to use COUNT(*) function with SELECT clause to count the total number of rows in a table.Examplemysql> Select COUNT(*) from Student; +----------+ | COUNT(*) | +----------+ | 4        | +----------+ 1 row in set (0.06 sec)The query above counts the total number of rows of ‘Student’ table.We can also use WHERE clause with COUNT(*) function as follows:mysql> Select COUNT(*) from Student where Address = 'Delhi'; +----------+ | COUNT(*) | +----------+ | 2        | +----------+ 1 row in set (0.00 sec)

What MySQL returns if we include time components along with date component as an argument to DATEDIFF() function?

Ramu Prasad
Updated on 30-Jan-2020 05:17:21

113 Views

MySQL DATEDIFF() function also works with date and time values but it ignores the time value. Hence even if we include the time value in DATEDIFF() function MySQL will return the difference, in days, between dates by ignoring the time values.mysql> Select DATEDIFF('2018-10-22 04:05:36', '2017-10-22 03:05:45'); +-------------------------------------------------------+ | DATEDIFF('2018-10-22 04:05:36', '2017-10-22 03:05:45') | +-------------------------------------------------------+ |                                                   365 | +-------------------------------------------------------+ 1 row in set (0.00 sec) mysql> Select DATEDIFF('2017-10-22 04:05:36', '2017-10-22 03:05:45'); +-------------------------------------------------------+ | DATEDIFF('2017-10-22 04:05:36', ... Read More

Advertisements