Found 4378 Articles for MySQL

Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query

AmitDiwan
Updated on 13-Dec-2019 07:04:30

170 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (3.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.78 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(70); ... Read More

Find second max in a table using MySQL query?

AmitDiwan
Updated on 13-Dec-2019 07:02:38

429 Views

You can use LIMIT 1 OFFSET 1. Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.92 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(2); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(204); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(5); Query OK, 1 row affected ... Read More

MySQL TINYINT type to return <>1 or IS NULL records

AmitDiwan
Updated on 13-Dec-2019 07:00:49

559 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY ,    -> EmployeeName varchar(20),    -> isMarried tinyint    -> ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName, isMarried) values('Chris', NULL); Query OK, 1 row affected (0.76 sec) mysql> insert into DemoTable(EmployeeName, isMarried) values('David', 1); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable(EmployeeName, isMarried) values('Mike', 0); Query OK, 1 row affected (0.69 sec) mysql> insert into DemoTable(EmployeeName, isMarried) values('Sam', NULL); Query OK, ... Read More

ERROR 1064 (42000): You have an error in your SQL syntax at zero fill column?

AmitDiwan
Updated on 13-Dec-2019 06:57:07

464 Views

Following is the error and it occurs when you implement ZEROFILL incorrectly−mysql> create table DemoTable    -> (    -> StudentCode int(10) NOT NULL ZEROFILL AUTO_INCREMENT PRIMARY KEY    -> ); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ZEROFILL AUTO_INCREMENT PRIMARY KEY )' at line 3For correct implementation, use the below syntax −SyntaxyourColumnName int(10) ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEYLet us first create a table −mysql> create table DemoTable    -> (    -> StudentCode int(10) ZEROFILL NOT NULL ... Read More

Find integer in text data (comma separated values) with MySQL?

AmitDiwan
Updated on 13-Dec-2019 06:55:11

262 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> DoubleValue varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(DoubleValue) values('80.2, 90.5, 88.90'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable(DoubleValue) values('78.56, 45.80, 88, 45.6'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(DoubleValue) values('12.34, 90.06, 89.90'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will ... Read More

How to sum a comma separated string (string with numbers) in MySQL?

AmitDiwan
Updated on 13-Dec-2019 06:53:01

898 Views

You can create a custom function to sum a comma-separated string in MySQL. Let us first create a table. Here, we have a varchar column, wherein we will add numbers in the form of strings −mysql> create table DemoTable    -> (    -> ListOfValues varchar(50)    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('20, 10, 40, 50, 60'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----------------+ | ListOfValues ... Read More

Get boolean result whether table exists or not using CASE WHEN in MySQL

AmitDiwan
Updated on 13-Dec-2019 06:50:08

245 Views

For this, you can use INFORMATION_SCHEMA.TABLES and find the table you want to search. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.71 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------+-------+ |   Id ... Read More

Concatenate columns from different tables in MySQL

AmitDiwan
Updated on 13-Dec-2019 06:48:11

2K+ Views

You can use CONCAT(). Let us first create a table −mysql> create table DemoTable1    -> (    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values('David'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+-----------+ | FirstName | +-----------+ | Chris     | | David     | +-----------+ 2 rows in set ... Read More

How to select a field corresponding to the field in which MAX() exists?

AmitDiwan
Updated on 13-Dec-2019 06:36:06

77 Views

For this, you can use sub query along with aggregate function MAX(). Let us first create a table −mysql> create table DemoTable    -> (    -> ProductId int,    -> ProductAmount int    -> ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1001, 7895); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(1003, 8903); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1010, 7690); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(2010, 8450); Query OK, 1 row ... Read More

Add an autoincrement column with a custom start value in MySQL

AmitDiwan
Updated on 13-Dec-2019 06:24:18

1K+ Views

To add a new column to an already created table, use ALTER TABLE and ADD COLUMN. Use AUTO_INCREMENT to set auto increment custom value.Let us first create a table −mysql> create table DemoTable    -> (    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.12 sec)Display all records from the table using ... Read More

Advertisements