Found 4381 Articles for MySQL

How to remove only the first word from columns values with a MySQL query?

AmitDiwan
Updated on 25-Sep-2019 10:18:51

689 Views

To remove only the first word from column values, use substring(). Following is the syntax −select substring(yourColumnName, locate(' ', yourColumnName)+1) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    Title text ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Java in Depth'); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable values('C++ is an object oriented programming language'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values('MySQL is a relational database'); Query OK, 1 row affected (0.17 ... Read More

Remove 20% from stored price in MySQL field, using SQL query?

AmitDiwan
Updated on 25-Sep-2019 10:16:53

137 Views

Let’s say the stored price is inclusive of 20% sales tax. Now, let us first create a table −mysql> create table DemoTable (    Price int ); Query OK, 0 rows affected (1.09 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select ... Read More

How to implement HAVING LENGTH(field) in MySQL?

AmitDiwan
Updated on 24-Sep-2019 14:14:14

133 Views

Let us first create a table −mysql> create table DemoTable (    Title text ); Query OK, 0 rows affected (0.39 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Introduction to Java'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Introduction to SQL'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Introduction to Python'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Introduction to SQL'); Query OK, 1 row affected (0.09 sec) ... Read More

Set options while creating a MySQL table. Display the same options as well

AmitDiwan
Updated on 24-Sep-2019 14:10:19

206 Views

To display, use DESC command or information_schema.columns. Let us first create a table and set options −mysql> create table DemoTable (    Color SET('RED', 'GREEN', 'BLUE', 'YELLOW') ); Query OK, 0 rows affected (0.47 sec)Case 1Here is the query to get the list of available options for SET using the DESC command −mysql> desc DemoTable;This will produce the following output −+-------+------------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | ... Read More

Inserting random numbers into a table in MySQL?

AmitDiwan
Updated on 24-Sep-2019 14:08:08

934 Views

To insert random numbers, use RAND() function from MySQL. Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.08 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | +-------+ | ... Read More

Delete a record with tablename.columnname= value in MySQL

AmitDiwan
Updated on 24-Sep-2019 14:04:14

136 Views

To delete a record in the required way, the syntax is as follows −delete from yourTableName where yourTableName .yourColumnName=yourValue;Let us first create a table −mysql> create table DemoTable (    StudentName varchar(100),    StudentAge int ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 21); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert', 20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David', 22); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Mike', 24); Query OK, 1 row ... Read More

Perform MySQL UPDATE on the basis of DATE value in another column

AmitDiwan
Updated on 24-Sep-2019 14:00:41

215 Views

Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100),    AdmissionDate date ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, AdmissionDate) values('Chris', '2019-11-21'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Name, AdmissionDate) values('Mike', '2019-03-11'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, AdmissionDate) values('Sam', '2018-04-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name, AdmissionDate) values('Carol', '2019-05-01'); Query OK, 1 row affected (0.40 sec)Display all records from the ... Read More

Copy values of one column to another using INSERT and SELECT in a single MySQL query

AmitDiwan
Updated on 25-Sep-2019 06:24:27

820 Views

Let us first create a table −mysql> create table DemoTable1 (    Name varchar(100),    Gender ENUM('MALE', 'FEMALE') ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values('Chris', 'Male'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values('Emma', 'Female'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+-------+--------+ | Name  | Gender | +-------+--------+ | Chris | MALE   | | Emma  | FEMALE | +-------+--------+ 2 rows in set ... Read More

Selecting all the users with maximum age values using a MySQL subquery?

AmitDiwan
Updated on 24-Sep-2019 13:54:18

365 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(100),    Age int ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('David', 55); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Bob', 53); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Mike', 54); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Sam', 55); Query OK, 1 row affected (0.18 sec)Display all records ... Read More

How to remove duplicate values from a MySQL table using LEFT JOIN?

AmitDiwan
Updated on 24-Sep-2019 13:43:38

591 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert ... Read More

Advertisements