Found 4378 Articles for MySQL

MySQL query to delete table rows if string in cell is matched

AmitDiwan
Updated on 25-Sep-2019 12:27:43

202 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100) ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Name) values('Carol Taylor'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name) values('John Doe'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Name) values('Chris Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Name) values('John Brown'); Query OK, 1 row ... Read More

Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers

AmitDiwan
Updated on 25-Sep-2019 12:25:42

195 Views

Let us first create a table −mysql> create table DemoTable (    Download varchar(100) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('120 Gigabytes'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('190 Gigabytes'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('250 Gigabytes'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('1000 Gigabytes'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----------------+ ... Read More

How to find a particular varchar id in MySQL from a list of values?

AmitDiwan
Updated on 25-Sep-2019 12:23:55

187 Views

To get a particular varchar ID from a list, you can use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable (    Id varchar(255) ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10, 100, 1000'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('101, 120, 2'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('3, 4, 5'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

Populate null columns in a MySQL table and set values

AmitDiwan
Updated on 25-Sep-2019 12:21:18

571 Views

For this, you can use IS NULL property. Let us first create a table −mysql> create table DemoTable (    ProductPrice int,    ProductQuantity int,    TotalAmount int ); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ProductPrice, ProductQuantity) values(100, 2); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(ProductPrice, ProductQuantity) values(500, 4); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(ProductPrice, ProductQuantity) values(1000, 10); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce ... Read More

How to make a pair of columns unique in MySQL?

AmitDiwan
Updated on 25-Sep-2019 12:18:45

627 Views

To make a pair of columns unique, use UNIQUE with ALTER TABLE command. Following is the syntax −alter table yourTableName add unique yourUniqueName(yourColumnName1, yourColumnName2, ...N);Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(100),    StudentLastName varchar(100),    StudentAge int,    StudentPhoneNumber varchar(20) ); Query OK, 0 rows affected (0.81 sec)Following is the query to make a pair of unique columns in MySQL −mysql> alter table DemoTable add unique DemoTable_unique_StudentFirstName_StudentPhoneNumber(StudentFirstName, StudentPhoneNumber); Query OK, 0 rows affected (0.40 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the ... Read More

Resolve ERROR 1064 (42000) that occurred after using varchar (without providing the size)

AmitDiwan
Updated on 25-Sep-2019 12:15:53

812 Views

Let us first see when this situation can arise. Create a table and set column name with datatype but without the size −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar,    LastName varchar ); 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 ', LastName varchar )' at line 4You can correct the above error by providing the size for varchar data type like varchar(100). The same will fix the issue.Let’s fix it ... Read More

While creating a MySQL table use the reserved keyword ‘Key’

AmitDiwan
Updated on 25-Sep-2019 12:14:19

105 Views

To use the reserved keyword ‘Key’, use the concept of the backtick symbol. Here, for our example, I am using the column name key which needs a backtick symbol around the column name.Let us first create a table −mysql> create table DemoTable (    `Key` int ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(101); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable values(110); Query OK, 1 row affected (0.28 sec) mysql> insert ... Read More

MySQL SUM function to add decimal values

AmitDiwan
Updated on 25-Sep-2019 12:12:07

1K+ Views

Let us first create a table −mysql> create table DemoTable (    Money DECIMAL(7, 2) ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100.67); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(199.33); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(500); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(400); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(800); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL query to select a record with two exact values?

AmitDiwan
Updated on 25-Sep-2019 12:08:56

140 Views

For this, you can use GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value int ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values(600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Value) values(600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Value) values(800); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Value) values(800); Query OK, 1 row affected (0.09 sec)Display all records from the table ... Read More

Display records on the basis of key-value pairs in MySQL

AmitDiwan
Updated on 25-Sep-2019 12:06:59

1K+ Views

For this, use JSON_OBJECTAGG(). Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(100),    Age int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'John', 23); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20, 'Carol', 21); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10, 'Sam', 24); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20, 'Chris', 20); Query OK, 1 row affected (0.13 sec)Display all records from the ... Read More

Advertisements