Found 4378 Articles for MySQL

Get the size of selected rows in MySQL

AmitDiwan
Updated on 17-Dec-2019 06:09:47

1K+ Views

To get the size of selected rows, use CHAR_LENGTH(). Let us first create a table −mysql> create table DemoTable1612    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1612 values('David', 'Brown'); Query OK, 1 row affected (0.75 sec) mysql> insert into DemoTable1612 values('John', 'Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1612 values('Bob', 'Taylor'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select * from DemoTable1612;This ... Read More

Combine multiple text records to one in MySQL

AmitDiwan
Updated on 17-Dec-2019 06:04:48

188 Views

To combine multiple text records, use GROUP_CONCAT(). Let us first create a table −mysql> create table DemoTable1611    -> (    -> Value text    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1611 values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1611 values('is'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1611 values('learning'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1611 values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1611 values('with'); Query OK, 1 row affected ... Read More

MySQL Order by a specific column x and display remaining values in ascending order

AmitDiwan
Updated on 26-Feb-2020 07:19:59

99 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> MonthNumber int    -> ); Query OK, 0 rows affected (1.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(6); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(5); ... Read More

Custom sorting using two different columns in MySQL?

AmitDiwan
Updated on 17-Dec-2019 06:01:17

147 Views

For this, use ORDER BY clause along with CASE statement. Let us first create a table −mysql> create table DemoTable1610    -> (    -> Marks int,    -> Name varchar(20)    -> ) ; Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1610 values(85, 'John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1610 values(78, 'Carol'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1610 values(78, 'John'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1610 values(85, 'Carol'); Query OK, 1 row affected ... Read More

Find specific records from a column with comma separated values in MySQL

AmitDiwan
Updated on 28-Feb-2020 07:28:16

718 Views

For this, you can use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable    -> (    -> ListOfValue varchar(20)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('78, 89, 65'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('88, 96, 97'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('95, 96, 99, 100'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('78, 45, 67, 98'); Query OK, 1 row affected (0.10 sec)Display all ... Read More

Add records from corresponding duplicate values in another column with MySQL

AmitDiwan
Updated on 28-Feb-2020 07:30:06

183 Views

For this, you can use the aggregate function SUM() along with the GROUP BY clause. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20),    -> Value int    -> ); Query OK, 0 rows affected (2.08 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 50); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David', 90); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Chris', 60); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Bob', 100); Query ... Read More

How to correctly use DELIMITER in a MySQL stored procedure?

AmitDiwan
Updated on 17-Dec-2019 05:59:31

232 Views

The correct way is as follows −DELIMITER // CREATE PROCEDURE yourStoredProcedureName() BEGIN  IF  yourCondition then      yourStatement1 ; else     yourStatement2 ; END IF ; END // DELIMITER ;Let us now see an example and create a stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE delimiter_demo()    -> BEGIN    -> IF 1 THEN    -> SELECT "If condition will always true";    -> else    -> select "No" ;    -> END IF ;    -> END    -> // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;Now you can call the ... Read More

Select the minimum value from the maximum values of two tables with a single MySQLquery?

AmitDiwan
Updated on 17-Dec-2019 05:58:03

108 Views

For this, you can use UNION in MySQL. Let us first create a table −mysql> create table DemoTable1    -> (    -> Value int    -> )    -> ; Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(60); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values(78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(57); Query OK, 1 row affected (0.08 sec)Display all records from the table using select statement −mysql> select * from DemoTable1; This will produce the following ... Read More

How to sum cells in a column if a condition is met in another column with MySQL?

AmitDiwan
Updated on 28-Feb-2020 07:31:19

813 Views

For this, you can use the aggregate function SUM() along with the GROUP BY clause. Let us first create a table −mysql> create table DemoTable    -> (    -> EmployeeName varchar(20),    -> JoiningDate date,    -> Salary int    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('David', '2019-11-02', 400); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable values('Robert', '2018-11-25', 100); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values('Bob', '2019-12-14', 600); Query OK, 1 row affected (0.25 sec) ... Read More

What are the minimum MySQL user privileges to allow optimize and repair?

AmitDiwan
Updated on 17-Dec-2019 05:51:08

349 Views

The select and insert statements are the minimum required MySQL user privileges to allow optimize and repair.You can use below syntax to give insert and select privileges to the user −grant insert, select on yourDatabaseName.* to 'yourUserName'@'localhost';At first, here is the query to create a user −mysql> create user 'Emma'@'localhost' identified by 'Emma123'; Query OK, 0 rows affected (0.26 sec)Here is the query to give grants for the above user −mysql> grant insert, select on web.* to 'Emma'@'localhost'; Query OK, 0 rows affected (0.21 sec)Here is the query to display all grants of the above user −mysql> show grants for ... Read More

Advertisements