Found 4378 Articles for MySQL

MySQL query to split a column after specific characters?

karthikeya Boyini
Updated on 30-Jun-2020 12:53:12

800 Views

To split a column after specific characters, use the SUBSTRING_INDEX() method −select substring_index(yourColumnName, '-', -1) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> StreetName text -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Paris Hill St.-CA-83745646') ; Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('502 South Armstrong Street-9948443'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------------------------------------+ | ... Read More

How to find the minimum values of two or more fields in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 12:54:44

574 Views

To find the minimum values of two or more fields, use LEAST() function from MySQL −select least(yourColumnName1, yourColumnName2, ...N) from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> Date1 date, -> Date2 date, -> Date3 date -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-03-31', '2019-01-01', '2019-03-05'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------------+------------+------------+ | Date1      | Date2      | Date3 ... Read More

How to get everything before the last occurrence of a character in MySQL?

Sharon Christine
Updated on 30-Jun-2020 12:55:53

498 Views

You can use below syntax. Following is the syntax −update yourTableName set yourColumnName=REVERSE(SUBSTRING(REVERSE(yourColumnName), INSTR(REVERSE(yourColumnName), '.')));Let us first create a table −mysql> create table DemoTable -> ( -> Words text -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Value1. Value2 .Value3.Value4.Value5'); Query OK, 1 row affected (0.22 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+--------------------------------------+ | Words ... Read More

Select a fixed number of random records from a MySQL table?

Sharon Christine
Updated on 30-Jun-2020 12:35:14

140 Views

For random records, you can use rand() method. To set the number of records, use the LIMIT −select *from yourTableName order by rand() limit numberOfRecords;Let us first create a table −mysql> create table DemoTable    -> (    -> LastName varchar(100)    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

MySQL query to return the entire date and time based on a string and format

Sharon Christine
Updated on 30-Jun-2020 12:36:03

67 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> AdmissionDate varchar(100)    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Wed, 19 Jun 2019 04:10:20'); Query OK, 1 row affected (0.22 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+---------------------------+ | AdmissionDate | +---------------------------+ | Wed, 19 Jun 2019 04:10:20 | +---------------------------+ 1 row in set (0.00 sec)Following is ... Read More

Get part of a string based on a character in MySQL?

Kumar Varma
Updated on 30-Jun-2020 12:39:04

100 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Code varchar(100)    -> ); Query OK, 0 rows affected (1.07 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('/101/102/106'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('/110/111/101'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('/111/114/201'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values('/111/118'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce ... Read More

Can we use ADD and CHANGE with ALTER Statement in MySQL?

Rama Giri
Updated on 30-Jun-2020 12:41:40

79 Views

Yes, we can use ADD and CHANGE with ALTER statement. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100),    -> Age int    -> ); Query OK, 0 rows affected (0.84 sec)Now check the description of table.mysql> desc DemoTable;OutputThis will produce the following output −+-------+--------------+------+-----+---------+-------+ | Field | Type         | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | Name  | varchar(100) | YES  |   | NULL |   | | Age   | int(11)   ... Read More

How to update data in a MySQL database without removing the old data?

Sharon Christine
Updated on 30-Jun-2020 12:39:56

980 Views

For this, you can use UPDATE and concatenate the new data with the old one to save the old data as well −update yourTableName set yourColumnName=concat(yourColumnName, ", yourValue");Let us first create a table −mysql> create table DemoTable -> ( -> CustomerName 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'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.14 sec)Display all records from ... Read More

Subquery to exclude a particular row in MySQL

Kumar Varma
Updated on 30-Jun-2020 12:42:49

298 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100),    -> Age int    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Age) values('John', 21); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name, Age) values('Carol', 22); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Age) values('David', 23); Query OK, 1 row affected (0.54 sec)Display all records from the table using select statement ... Read More

MySQL query to search between comma separated values within one field?

karthikeya Boyini
Updated on 30-Jun-2020 12:45:13

394 Views

Use FIND_IN_SET for this. Let us first create a table −mysql> create table DemoTable -> ( -> ListOfValues text -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10|20|30|40|50|60|100'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-----------------------+ | ListOfValues | +-----------------------+ | 10|20|30|40|50|60|100 | +-----------------------+ 1 row in set (0.00 sec)Following is the query to search between comma separated values within one ... Read More

Advertisements