Found 4219 Articles for MySQLi

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

MySQL query error with a table named “order”?

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

327 Views

The order is a reserved word. To still use a reserved word, you need to use backticks around the column name. Let us first create a table −mysql> create table `order`    -> (    -> StudentId int    -> ); Query OK, 0 rows affected (1.78 sec)Insert some records in the table using insert command −mysql> insert into `order` values(101); Query OK, 1 row affected (0.26 sec) mysql> insert into `order` values(210); Query OK, 1 row affected (0.18 sec) mysql> insert into `order` values(190); Query OK, 1 row affected (0.28 sec) mysql> insert into `order` values(180); Query OK, 1 ... Read More

Show row with zero value after addition in MySQL?

AmitDiwan
Updated on 13-Dec-2019 06:16:40

111 Views

For this, you can use an aggregate function SUM() along with the condition. Let us first create a table −mysql> create table DemoTable    -> (    -> Status varchar(20)    -> ); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.18 sec)Display all records from the ... Read More

Find rows with a match in a pipe delimited column with MySQL

AmitDiwan
Updated on 13-Dec-2019 06:14:20

385 Views

To find a match, use regular expressions in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Value varchar(60)    -> ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('8|56|78|45'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('9876'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('98|8'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('3|8|9'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('97|94'); Query OK, 1 ... Read More

How to add a string containing double quote to records in MySQL?

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

395 Views

To insert records with double quotes, use a backslash (\) as in the below syntax −Syntaxinsert into yourTableName values('\"yourValue\"');Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('\"John\"'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('\"Chris\"'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('\"Adam Smith\"'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('\"Carol\"'); Query OK, 1 row affected ... Read More

MySQL search and replace record from a list of records

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

110 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> ListOfName text    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Carol, Sam, John, David, Bob, Mike, Robert, John, Chris, James, Jace'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------------------------------------------------------+ | ListOfName                                               ... Read More

Advertisements