Found 4219 Articles for MySQLi

Add a column to a MySQL table which is the result of concatenation of text and value from another auto increment column?

AmitDiwan
Updated on 20-Aug-2019 09:09:39

398 Views

For this, you can use LAST_INSERT_ID(). Let us first create a table −mysql> create table DemoTable    (    UserId int(6) unsigned zerofill NOT NULL AUTO_INCREMENT,    UserAutoIncrement char(100) default null,    PRIMARY KEY(UserId)    ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+--------+-------------------+ | UserId | UserAutoIncrement | +--------+-------------------+ | 000001 | NULL | +--------+-------------------+ ... Read More

Round seconds to nearest half minute in MySQL?

AmitDiwan
Updated on 02-Jul-2020 06:45:14

230 Views

To round seconds to nearest half minute, use CEILING(). Let us first create a table −mysql> create table DemoTable (secondValue int); Query OK, 0 rows affected (0.64 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values(27); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(118); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------------+ | secondValue | +-------------+ | 27          | | 56       ... Read More

How can I get the number of times a specific word appears in a column with MySQL?

Sharon Christine
Updated on 30-Jun-2020 14:59:08

1K+ Views

For this, you can use COUNT() function. Let us first create a table −mysql> create table DemoTable    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(100)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(EmployeeName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(EmployeeName) values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(EmployeeName) values('Carol'); Query OK, 1 row affected ... Read More

Add values of two columns considering NULL values as zero in MySQL

Sharon Christine
Updated on 30-Jun-2020 15:00:17

764 Views

For this, use COALESCE() function from MySQL. Let us first create a table −mysql> create table DemoTable -> ( -> Value1 int, -> Value2 int -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 200); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(NULL, 90); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(NULL, NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(600, NULL); Query OK, 1 row affected (0.12 sec)Display all records from ... Read More

Increase database field value by specified percentage using user-defined variables in MySQL

karthikeya Boyini
Updated on 30-Jun-2020 15:01:18

296 Views

Let us first create a table −mysql> create table DemoTable -> ( -> Amount int -> ); Query OK, 0 rows affected (0.99 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(500); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+--------+ | Amount | +--------+ | 100 | | 200 ... Read More

MySQL query to add dots if string has more than 10 words?

karthikeya Boyini
Updated on 30-Jun-2020 15:04:24

275 Views

For this, use CASE statement. Let us first create a table −mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('My name is John and this is my first tutorial on MySQL'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('This is Carol and I work on MongoDB'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+--------------------------------------------------------+ | Title ... Read More

MySQL stored procedure to return a column value?

Sharon Christine
Updated on 30-Jun-2020 15:05:27

809 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Score int    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 858858686); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(2, 9900554); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(3, 646565667); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------+-----------+ | Id ... Read More

Swap data between two columns in MySQL?

Sharon Christine
Updated on 30-Jun-2020 15:06:56

911 Views

To swap data between two columns in MySQL, use the concept of variable. Let us first create a table. Here, we will swap Name1 with Name2 −mysql> create table DemoTable -> ( -> Name1 varchar(100), -> Name2 varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John Smith', 'Chris Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David Miller', 'Jone Doe'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce ... Read More

MySQL query to increment one of the column values

karthikeya Boyini
Updated on 30-Jun-2020 14:43:15

1K+ Views

Let us first create a table −mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100), -> Score int -> ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Score) values('John', 68); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(Name, Score) values('Carol', 98); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(Name, Score) values('David', 89); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name, Score) values('Robert', 67); Query OK, 1 row affected ... Read More

MySQL query to first set negative value in descending order and then positive value in ascending order

karthikeya Boyini
Updated on 30-Jun-2020 14:45:28

408 Views

For this, you can use UNION. Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (1.17 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(-9); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(-190); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(190); Query OK, 1 row affected ... Read More

Advertisements