Found 6705 Articles for Database

MySQL query to update only month in date?

AmitDiwan
Updated on 03-Sep-2019 12:49:39

925 Views

To update only month in date, use MONTH(). Let us first create a table −mysql> create table DemoTable861(AdmissionDate date); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable861 values('2019-01-21'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable861 values('2018-01-01'); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable861 values('2016-01-02'); Query OK, 1 row affected (1.27 sec) mysql> insert into DemoTable861 values('2018-01-14'); Query OK, 1 row affected (0.47 sec)Display all records from the table using select statement −mysql> select *from DemoTable861;This will produce the following output −+---------------+ ... Read More

How to order by the highest value from two columns in MySQL?

AmitDiwan
Updated on 03-Sep-2019 12:48:44

570 Views

Let us first create a table −mysql> create table DemoTable834(    Value1 int,    Value2 int ); Query OK, 0 rows affected (1.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable834 values(10, 20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable834 values(40, 50); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable834 values(20, 24); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable834 values(30, 10); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable834;This will produce the following ... Read More

How to skip rows in MySQL?

AmitDiwan
Updated on 03-Sep-2019 12:47:17

4K+ Views

To skip rows, use LIMIT OFFSET. Let us first create a table −mysql> create table DemoTable860(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100) ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable860(Name) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable860(Name) values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable860(Name) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable860(Name) values('Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable860(Name) values('Mike'); Query OK, 1 row ... Read More

How to make numbers 'human readable' in MySQL?

AmitDiwan
Updated on 03-Sep-2019 12:43:41

469 Views

To make numbers ‘human readable’ i.e. including separators, you need to use format(). Let us first create a table −mysql> create table DemoTable859(Number bigint); Query OK, 0 rows affected (1.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable859 values(747464646473737); Query OK, 1 row affected (0.45 sec) mysql> insert into DemoTable859 values(3836365366464); Query OK, 1 row affected (0.71 sec) mysql> insert into DemoTable859 values(8376437647433); Query OK, 1 row affected (0.46 sec)Display all records from the table using select statement −mysql> select *from DemoTable859;This will produce the following output −+-----------------+ | Number          | ... Read More

How to subtract seconds from all the date records in a MySQL column?

AmitDiwan
Updated on 03-Sep-2019 12:44:48

408 Views

Let us first create a table −mysql> create table DemoTable833(ArrivalTime datetime); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command −mysql> insert into DemoTable833 values('2019-01-10 12:40:50'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable833 values('2019-02-16 11:10:30'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable833 values('2018-03-17 10:00:10'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable833;This will produce the following output −+---------------------+ | ArrivalTime | +---------------------+ | 2019-01-10 12:40:50 | | ... Read More

Getting the first part from the Postcode in MySQL

AmitDiwan
Updated on 03-Sep-2019 12:41:17

221 Views

To get the first part, use SUBSTRING(). Let us first create a table −mysql> create table DemoTable858(PostCode varchar(100)); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable858 values('US90 456'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable858 values('UK1 EN789343'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable858 values('ENG78884 736454654'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable858;This will produce the following output −+--------------------+ | PostCode           | +--------------------+ | ... Read More

Insert non-duplicates value in a MySQL table

AmitDiwan
Updated on 03-Sep-2019 12:40:19

212 Views

To allow inserting only non-duplicate values, use the UNIQUE constraint. Let us first create a table −mysql> create table DemoTable832(    FirstName varchar(100),    LastName varchar(100),    UNIQUE(FirstName, LastName) ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable832 values('John', 'Smith'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable832 values('Adam', 'Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable832 values('John', 'Doe'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable832 values('John', 'Smith'); ERROR 1062 (23000): Duplicate entry 'John-Smith' for key 'FirstName'Above, we ... Read More

How to create a table from view in MySQL?

AmitDiwan
Updated on 03-Sep-2019 12:37:49

601 Views

To create a table from the view below is the syntax −create table yourTableName select *from yourViewName;Let us first create a table −mysql> create table DemoTable830(Name varchar(100)); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable830 values('Chris'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable830 values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable830 values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable830 values('Mike'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select ... Read More

Remove decimal from records and sum them using a MySQL query

AmitDiwan
Updated on 03-Sep-2019 12:38:48

477 Views

Use aggregate function SUM() along with REPLACE(). Let us first create a table −mysql> create table DemoTable857(Price varchar(100)); Query OK, 0 rows affected (1.40 sec)Insert some records in the table using insert command −mysql> insert into DemoTable857 values('50.60'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable857 values('40.30.20'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable857 values('10.20'); Query OK, 1 row affected (0.69 sec)Display all records from the table using select statement −mysql> select *from DemoTable857;This will produce the following output −+----------+ | Price    | +----------+ | 50.60    | | 40.30.20 | | ... Read More

MySQL left padding with zeros separated by hyphen?

AmitDiwan
Updated on 03-Sep-2019 12:28:57

169 Views

Let us first create a table −mysql> create table DemoTable829(SerialNumber int); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable829 values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable829 values(101); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable829 values(102); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable829 values(103); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable829 values(104); Query OK, 1 row affected (0.60 sec)Display all records from the table using select statement −mysql> select *from DemoTable829;This will produce ... Read More

Advertisements