Found 4381 Articles for MySQL

How can we specify default values in MySQL INSERT statement?

Lakshmi Srinivas
Updated on 30-Jan-2020 05:58:13

2K+ Views

At the time of creation of a table, if any column is defined with default values then by using the keyword ‘DEFAULT’ in the INSERT statement, we can take default value for that column. For example, we have created a table ‘employee’ with a default value of column ‘DOJ’ as follows −mysql> Create table employee(id int, name varchar(20), doj date DEFAULT '2005-01-01'); Query OK, 0 rows affected (0.09 sec) mysql> Insert into employee(id, name, doj) values(1, ’Aarav’, DEFAULT); Query OK, 1 row affected (0.03 sec) mysql> select * from employee; +------+------------+---------------+ | id   | name     ... Read More

How can we use MySQL function STR_TO_DATE(Column, ‘%input_format’)?

Ramu Prasad
Updated on 30-Jan-2020 05:59:04

387 Views

STR_TO_DATE() function will convert a string value into datetime value and it would be according to a specific format string. Both string value and format string must be passed as arguments to the function. Following is the syntax of STR_TO_DATE() function.STR_TO_DATE(string, format)Here string is the value of string which needs to be converted to datetime value and format is the specified date format.The following example will return valid date from given string and according to the specified format.mysql> Select STR_TO_DATE('20172810', '%Y%d%m'); +-----------------------------------+ | STR_TO_DATE('20172810', '%Y%d%m') | +-----------------------------------+ | 2017-10-28                       ... Read More

How to use MySQL FROM_UNIXTIME() function to return datetime value in numeric format?

Sreemaha
Updated on 30-Jan-2020 05:59:45

254 Views

As we know that we can convert a time of datetime value to an integer by adding 0(+0) to them. In a similar way, we can convert the datetime value returned by FROM_UNIXTIME() function in numeric format. The following example will clarify it more −mysql> Select FROM_UNIXTIME(1555033470)+0 AS 'Date in Numeric Format'; +------------------------+ | Date in Numeric Format | +------------------------+ | 20190412071430.000000  | +------------------------+ 1 row in set (0.00 sec)After adding 0 (+0) to datetime value MySQL returns the numeric value up to 6 digits microseconds.

How can we update values in a MySQL table?

Rama Giri
Updated on 30-Jan-2020 06:00:27

563 Views

With the help of UPDATE statement and WHERE clause, we can update the values in single or multiple rows of the table. MySQL updates the values on the basis of condition specified in WHERE clause. For example, suppose in the ‘employee’ table we want to change the ‘name’ and ‘doj’ of the employee whose id is 1 then it can be done with the following query −mysql> UPDATE employee SET name = 'Gaurav', doj = '2010-02-01' WHERE id = 1; Query OK, 1 row affected (0.06 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from employee ... Read More

How can we update columns values on multiple rows with a single MySQL UPDATE statement?

Jai Janardhan
Updated on 20-Jun-2020 07:03:14

4K+ Views

Column values on multiple rows can be updated in a single UPDATE statement if the condition specified in WHERE clause matches multiple rows. In this case, the SET clause will be applied to all the matched rows.ExampleSuppose we have a table ‘tender’ as follows −mysql> Select * from tender; +-----------+---------+------+ | tender_id | company | rate | +-----------+---------+------+ | 200       | ABC     | 1000 | | 300       | ABD     | 5000 | | 301       | ABE     | 6000 | | 302       ... Read More

What is the significance of using multiple columns in MySQL GROUP BY clause?

Ankith Reddy
Updated on 06-Feb-2020 06:45:51

207 Views

By specifying multiple columns in GROUP BY clause we can split the result set into smaller groups. The more columns specified in GROUP BY clause, the smaller the groups will be.Examplemysql> Select designation, YEAR(Doj), count(*) from employees GROUP BY designation, YEAR(DoJ); +-------------+-----------+----------+ | designation | YEAR(Doj) | count(*) | +-------------+-----------+----------+ | Asso.Prof   | 2013      | 1        | | Asst.Prof   | 2015      | 1        | | Asst.Prof   | 2016      | 1        | | Prof        | 2009      | 2        | | Prof        | 2010      | 1        | +-------------+-----------+----------+ 5 rows in set (0.00 sec)

In MySQL, how can we use FROM_UNIXTIME() function with format string?

Sravani S
Updated on 20-Jun-2020 06:41:59

202 Views

Suppose if we want the output of FROM_UNIXIME() function in a particular format then we can use date format string or time format string or both in it. Following is the example of using the format string in FROM_UNIXTIME() function −mysql> Select FROM_UNIXTIME(1555033470 '%Y %M %D')AS 'Formatted Output'; +------------------+ | Formatted Output | +------------------+ | 2019 April 12th  | +------------------+ 1 row in set (0.00 sec)In the query above, it is using only date format string.mysql> Select FROM_UNIXTIME(1555033470 '%h:%i:%s')AS 'Formatted Output'; +------------------+ | Formatted Output | +------------------+ | 07:14:30         | +------------------+ 1 row in set (0.00 ... Read More

What MySQL returns if we use UNIX_TIMESTAMP() function with no argument?

V Jyothi
Updated on 20-Jun-2020 06:41:07

280 Views

In that case, MySQL returns the Unix timestamp of the current date and time. Hence we can say that using no argument is same as using NOW() as an argument to UNIX_TIMESTAMP() function.For example, if we run the query for UNIX_TIMESTAMP() with no value and with NOW() as an argument that MySQL returns the same result.mysql> Select UNIX_TIMESTAMP(); +------------------+ | UNIX_TIMESTAMP() | +------------------+ |       1509405559 | +------------------+ 1 row in set (0.00 sec) mysql> Select UNIX_TIMESTAMP(NOW()); +-----------------------+ | UNIX_TIMESTAMP(NOW()) | +-----------------------+ |            1509405559 | +-----------------------+ 1 row in set (0.00 sec)The ... Read More

How can we fetch month and day from a given date in MySQL?

usharani
Updated on 30-Jan-2020 05:37:11

170 Views

It can be done inflowing two ways −(A) With the help of EXRACT() function - EXTRACT() function can fetch any part from MySQL TIMESTAMP value. Following is the example of fetching month and day from a given date.mysql> Select EXTRACT(Month from '2017-10-22') AS 'MONTH'; +-------+ | MONTH | +-------+ |    10 | +-------+ 1 row in set (0.00 sec) mysql> Select EXTRACT(day from '2017-10-22')AS 'DAY'; +------+ | DAY  | +------+ |   22 | +------+ 1 row in set (0.00 sec)(B) With the help of MONTH() or DAY() function -  Rather than passing month and day as one of ... Read More

How can I use TIME_FORMAT() function to offload time/date values in MySQL?

Priya Pallavi
Updated on 30-Jan-2020 05:39:56

136 Views

TIME_FORMAT() function can be used in a similar fashion as DATE_FORMAT() function but it can only be used for offloading time values. MySQL returns a NULL value if TIME_FORMAT() function is used for offloading date values.For example, when we pass the time format units as arguments to MySQL TIME_FORMAT() function then MySQL offloaded only the time as follows −mysql> Select TIME_FORMAT("2017-10-22 13:03:45", "%h %i %s %p")AS 'OFFLOADED TIME'; +----------------+ | OFFLOADED TIME | +----------------+ | 01 03 45 PM    | +----------------+ 1 row in set (0.00 sec)Whereas, when we pass the date format units as arguments to MySQL TIME_FORMAT() ... Read More

Advertisements