Found 4378 Articles for MySQL

How to update only day portion of MySQL Date?

Sharon Christine
Updated on 30-Jun-2020 11:21:41

1K+ Views

Let us first create a table −mysql> create table DemoTable -> ( -> AdmissionDate date -> ); Query OK, 0 rows affected (1.38 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-05-12'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable values('2019-05-18'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-04-19'); Query OK, 1 row affected (0.42 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+---------------+ | AdmissionDate | +---------------+ | 2019-05-12 | | 2019-05-18 ... Read More

Pad the displayed value of the field with zeros up to the display width specified in the column definition in MySQL?

Sharon Christine
Updated on 30-Jun-2020 11:23:59

59 Views

Use zerofill for this in MySQL. Zerofill pads the displayed value of the field with zeros up to the display width specified in the column definition. For example, if column is set int(8), therefore the width is 8. If the number is let’s say 29654, then zero will be padded on the left for total width i.e.8 −00029654Let us first create a table −mysql> create table DemoTable -> ( -> Number int(8) zerofill -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected ... Read More

Fetch a single ordered date from a column with MySQL LIMIT

Sharon Christine
Updated on 30-Jun-2020 11:24:45

79 Views

To fetch a single date from a column, use “LIMIT 1. To order it, use ORDER BY clause. Let us first create a table −mysql> create table DemoTable -> ( -> DueDate varchar(100) -> ); Query OK, 0 rows affected (1.16 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10-06-2019'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable values('01-12-2016'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable values('31-01-2018'); Query OK, 1 row affected (0.58 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis ... Read More

Fetch records containing a specific character twice in MySQL

Rama Giri
Updated on 30-Jul-2019 22:30:26

205 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Words text    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Ever'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Forever'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Good'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Never'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+---------+ | Words ... Read More

How to get the data associated with the maximum id in a MySQL table?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

74 Views

We will first order by DESC and then fetch the value associated with maximum id −select *from yourTableName order by yourColumnName DESC LIMIT 1, 1;Let us first create a table −mysql> create table DemoTable    -> (    -> Alldata int    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(303); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(560); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.13 sec) mysql> insert ... Read More

Count the number of distinct values in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

164 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100),    -> Code varchar(100)    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', '0001'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Robert', '0002'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert', '0003'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris', '0001'); Query OK, 1 row affected (0.12 sec)Display all records from the table using ... Read More

MySQL query to select rows except first row in descending order?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

709 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Amount int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+--------+ | Amount ... Read More

MySQL query to delete all rows older than 30 days?

Rama Giri
Updated on 30-Jul-2019 22:30:26

18K+ Views

To delete all rows older than 30 days, you need to use the DELETE with INTERVAL. Use < now() i.e. less than operator to get all the records before the current date.Let us first create a table −mysql> create table DemoTable    -> (    -> UserMessage text,    -> UserMessageSentDate date    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Hi', '2019-06-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Hello', '2019-07-02'); Query OK, 1 row affected (0.14 sec) mysql> insert ... Read More

STR_TO_DATE as column, but column not found?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

68 Views

You can use having clause. Let us first create a table −mysql> create table DemoTable    -> (    -> AdmissionDate varchar(100)    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10/12/2017'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('01/11/2018'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('31/01/2019'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('09/06/2019'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('19/04/2019'); Query OK, 1 ... Read More

How to check if a datetime value equals tomorrows date in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

198 Views

For this, you can use DATEDIFF(). Let us first create a table −mysql> create table DemoTable -> ( -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-07-01'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('2019-07-02'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-07-03'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('2019-07-04'); Query OK, 1 row affected (0.15 sec)Display all records ... Read More

Advertisements