Found 4218 Articles for MySQLi

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

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

162 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

147 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

1K+ 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

402 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

481 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

How to concatenate MySQL distinct query results into a string?

Rama Giri
Updated on 30-Jun-2020 12:45:56

1K+ Views

Use group_concat() function from MySQL to concatenate. Let us first create a table −mysql> create table DemoTable    -> (    -> Subject varchar(10)    -> ); Query OK, 0 rows affected (0.43 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('C'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('C++'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('C++'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values('MongoDB'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('MySQL'); Query ... Read More

Order by number of chars in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 12:46:58

469 Views

To order by number of chars, use ORDER BY and LENGTH() method. Following is the syntax −select *from yourTableName order by LENGTH(yourColumnName) DESC;Let us first create a table −mysql− create table DemoTable    -> (    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable ... Read More

How to add current date to an existing MySQL table?

karthikeya Boyini
Updated on 30-Jun-2020 12:23:57

395 Views

To update an existing table, use UPDATE. With that, to set the current date, use the CURDATE() method −update yourTableName set yourCoumnName=CURDATE();Let us first create a table −mysql> create table DemoTable -> ( -> DueDate datetime -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-10') ; Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('2019-03-31'); 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 −+---------------------+ | DueDate ... Read More

How to select everything before @ in an email-id with in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 12:25:33

767 Views

Use SUBSTRING_INDEX to select everything before @ in an email-id −select substring_index(yourColumnName, '@', 1) from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> EmployeeMailId varchar(100) -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Larry123@gmail.com'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('987Sam@hotmail.com'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('123456David_98@gmail.com'); 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 ... Read More

How to set default Field Value in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 12:27:04

687 Views

To set default field value, use the “default”. Let us first create a table −mysql> create table DemoTable -> ( -> Age int -> ); Query OK, 0 rows affected (0.58 sec)Here is the query to set default field value in MySQL −mysql> alter table DemoTable MODIFY Age int default 18; Query OK, 0 rows affected (0.25 sec) Records: 0 Duplicates: 0 Warnings: 0Now you can check the table description −mysql> desc DemoTable;OutputThis will produce the following output −+-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | Age ... Read More

Advertisements