Found 4378 Articles for MySQL

MySQL order by field using CASE Statement

AmitDiwan
Updated on 22-Aug-2019 11:17:21

707 Views

To order by field, use CASE statement. Let us first create a table −mysql> create table DemoTable(StudentId varchar(100)); Query OK, 0 rows affected (1.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('STU-980'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('STU-1029'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('STU-189'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('STU-890'); Query OK, 1 row affected (0.38 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | ... Read More

Getting count of two different sets of rows in a table and then dividing them in MySQL

AmitDiwan
Updated on 22-Aug-2019 11:13:36

743 Views

For this, use count(*) and the divide the count of two different sets of rows. Let us first create a table −mysql> create table DemoTable(isMarried tinyint(1)); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.17 ... Read More

Copy all rows of a table to another table in MySQL?

AmitDiwan
Updated on 22-Aug-2019 11:05:17

636 Views

To copy all rows of a table to another table, use the below syntax −insert into yourTableName2(yourColumnName1, ...N) select yourColumnName1, ..N from yourTableName1;Let us first create a table −mysql> create table DemoTable1(FirstName varchar(100)); Query OK, 0 rows affected (1.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values('John'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1 values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values('Bob'); Query OK, 1 row affected (0.40 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following ... Read More

MySQL query to sum the values of similar columns from two different tables for a particular ID

AmitDiwan
Updated on 22-Aug-2019 11:02:31

374 Views

Let’s say we have two tables and both of them have two columns PlayerId and PlayerScore. We need to add the PlayerScore from both these tables, but only for a particular PlayerId.For this, you can use UNION. Let us first create a table −mysql> create table DemoTable1(PlayerId int, PlayerScore int); Query OK, 0 rows affected (9.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(1000, 87); Query OK, 1 row affected (3.12 sec) mysql> insert into DemoTable1 values(1000, 65); Query OK, 1 row affected (1.29 sec) mysql> insert into DemoTable1 values(1001, 10); Query OK, 1 ... Read More

Filter query by current date in MySQL

AmitDiwan
Updated on 22-Aug-2019 10:57:51

1K+ Views

Let us first create a table −mysql> create table DemoTable(DueDate datetime); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-07-10 04:20:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-07-10 05:10:40'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-07-10 09:00:20'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-07-10 10:01:04'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2019-07-10 12:11:10'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> ... Read More

Using CREATE TABLE AS statement with UNION of two tables in MySQL

AmitDiwan
Updated on 22-Aug-2019 10:54:03

2K+ Views

For this, you can use UNION. Let us first create a table −mysql> create table DemoTable1(FirstName varchar(1000)); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values('John'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+-----------+ | FirstName | +-----------+ | John      | +-----------+ 1 row in set (0.02 sec)Here is the query to create second the table −mysql> create table DemoTable2(FirstName varchar(100)); Query OK, 0 rows affected (0.81 sec)Insert some records ... Read More

How to check an empty table already in a MySQL database?

AmitDiwan
Updated on 22-Aug-2019 10:48:25

1K+ Views

To check an empty table is in a database, you need to extract some records from the table. If the table is not empty then the table records would be returned.Let us first create a table −mysql> create table DemoTable(Id int, Name varchar(100), Age int); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1001, 'John', 23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(1002, 'Chris', 21); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(1003, 'David', 22); Query OK, 1 row affected ... Read More

How to order by auto_increment in MySQL?

AmitDiwan
Updated on 22-Aug-2019 10:42:20

451 Views

Let us first create a table −mysql> create table DemoTable(Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100)); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, ... Read More

Facing difficulty in removing the apostrophe in MySQL stored procedure?

AmitDiwan
Updated on 22-Aug-2019 10:26:46

205 Views

To remove apostrophe, replace it. For this, you can use REPLACE(). Following is the syntax −SET anyVariableName = REPLACE(yourVaribleName , '\'', '');To understand the above syntax, let us create a stored procedure to remove the apostrophe in MySQL −mysql> DELIMITER // mysql> CREATE PROCEDURE remove_Apostrophe(IN Value VARCHAR(200))    BEGIN       SET Value = REPLACE(Value , '\'', '');       SELECT CONCAT("AFTER REMOVING APOSTROPHE THE STRING IS= ", Value);    END    // Query OK, 0 rows affected (0.15 sec) mysql> DELIMITER ;Call the stored procedure using CALL command −mysql> CALL remove_Apostrophe("Introduction to My'SQL");This will produce the following ... Read More

How to get all the MySQL triggers and the triggers for only the current database

AmitDiwan
Updated on 22-Aug-2019 09:02:23

192 Views

To get all the MySQL triggers, following is the syntax −select trigger_schema, trigger_name from information_schema.triggers;Let us implement the above syntax to get all the trigger names along with schema −mysql> select trigger_schema, trigger_name from information_schema.triggers;This will produce the following output −+----------------+---------------------------------+ | TRIGGER_SCHEMA | TRIGGER_NAME                    | +----------------+---------------------------------+ | sys            | sys_config_insert_set_user      | | sys            | sys_config_update_set_user      | | business       | binsertTrigger                  | | business     ... Read More

Advertisements