Found 4378 Articles for MySQL

Store a variable with the result of a MySQL SELECT CASE?

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

824 Views

For this, use SELECT CASE. Let us first create a table −mysql> create table DemoTable    -> (    -> Num1 int,    -> Num2 int    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 30); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------+------+ | Num1 | Num2 | +------+------+ |   10 |   30 | +------+------+ 1 row in set (0.00 sec)Following is the query to store ... Read More

Can we use “IF NOT IN” in a MySQL procedure?

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

101 Views

Let us first see the syntax of IF NOT IN in MySQL −if(yourVariableName  NOT IN (yourValue1, yourValue2, ........N) ) then    statement1 else    statement2 endif    Let us implement the above syntax to use IF NOT IN −mysql> DELIMITER // mysql> CREATE PROCEDURE IF_NOT_INDemo(IN value int)    ->    BEGIN    ->       if(value NOT IN  (10, 20, 30) ) then    ->          select "Value Not Found";    ->       else    ->          select "Value Found";    ->       end if;    ->    END ... Read More

Count rows having three or more rows with a certain value in a MySQL table

Rama Giri
Updated on 30-Jul-2019 22:30:26

87 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> UserId int    -> ); Query OK, 0 rows affected (0.48 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.12 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.09 sec) ... Read More

Select last record and update it in MySQL?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

2K+ Views

For this, you can use ORDER BY DESC LIMIT. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Name) values('Carol'); Query OK, 1 row affected (0.34 sec)Display all records from the table using select statement −mysql> ... Read More

AUTO_INCREMENT in MySQL can be signed by default?

Rama Giri
Updated on 30-Jul-2019 22:30:26

232 Views

Yes, the AUTO_INCREMENT in MySQL will be signed (Positive and Negative Value both) by default.Let us first create a table −mysql> create table DemoTable    -> (    -> MyNumber int AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command. Here, we have set negative values as well for AUTO_INCREMENT column −mysql> insert into DemoTable values() ; Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(-100); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(-300); Query OK, 1 row affected ... Read More

How to order an alphanumeric column in MySQL?

Sharon Christine
Updated on 30-Jun-2020 12:16:32

1K+ Views

To order an alphanumeric column with values like “100X, “2Z”, etc. use the ORDER BY. Let us first create a table −mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2X'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('100Y'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('100X'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2Z'); Query OK, 1 row affected (0.14 sec) mysql> ... Read More

Multiply values of two columns and display it a new column in MySQL?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

3K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> NumberOfItems int,    -> Amount int    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(4, 902); Query OK, 1 row affected (0.45 sec) mysql> insert into DemoTable values(5, 1000); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(3, 80); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+---------------+--------+ | NumberOfItems | Amount | +---------------+--------+ |   ... Read More

How to combine two tables and add a new column with records in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

452 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(Name) values('Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1(Name) values('Robert'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+----+--------+ | Id | Name   | +----+--------+ |  1 | Chris  | |  2 | Robert | +----+--------+ 2 ... Read More

How to get row count of two tables in different databases in a single query?

karthikeya Boyini
Updated on 30-Jun-2020 12:21:37

330 Views

For this, you can use aggregate function COUNT(*). Let us first create a table in let’s say database “web” −mysql> create table DemoTable1    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1 values(20); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;OutputThis will produce the following output −+-------+ | Value | +-------+ |    10 | | ... Read More

Update table with duplicate ids in MySQL

Kumar Varma
Updated on 30-Jul-2019 22:30:26

504 Views

Following is the syntax −update yourTableName set yourColumnName1= yourValue where yourColumnName2=yourValue order by yourColumnName2 DESC LIMIT 1;Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 'John'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(2, 'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(2, 'Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into ... Read More

Advertisements