Found 4378 Articles for MySQL

Delete records from a MySQL table with IN() in a single query

AmitDiwan
Updated on 30-Dec-2019 07:16:08

154 Views

Let us create a table −mysql> create table DemoTable1922    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1922(StudentName) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1922(StudentName) values('Robert'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1922(StudentName) values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1922(StudentName) values('Mike'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1922;This ... Read More

Fix a specific column value and display random values for rest of the rows in MySQL

AmitDiwan
Updated on 30-Dec-2019 07:14:43

114 Views

For random rows, you can use RAND(), whereas to fix a specific column, use ORDER BY clause. Let us create a table −mysql> create table DemoTable1921    (    Number int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1921 values(40); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1921 values(80); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1921 values(820); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1921 values(10); Query OK, 1 row affected (0.00 sec)Display all records from the ... Read More

Group the marks of a particular student from a table and display total marks in a separate column for each student?

AmitDiwan
Updated on 30-Dec-2019 07:11:27

9K+ Views

To group marks, use MySQL GROUP BY. To sum, use MySQL sum()function. Let us first create a table −mysql> create table DemoTable1920    (    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1920 values('Chris', 67); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1920 values('David', 97); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1920 values('Chris', 57); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1920 values('David', 45); Query OK, 1 row affected (0.00 sec) mysql> ... Read More

Using DECLARE to create variable in MySQL?

AmitDiwan
Updated on 30-Dec-2019 07:09:30

457 Views

You can use DECLARE in a stored procedure. The syntax is as follows −declare yourVariableName yourDataType;To understand the above syntax, let us create a stored procedure:mysql> delimiter // mysql> create procedure square_demo(in Value int)    begin    declare magicValue int;    set magicValue=Value;    select concat('Your Square Value=',magicValue*magicValue) as Output;    end ;    // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ;Now you can call a stored procedure using call command −mysql> call square_demo(15);This will produce the following output −+-----------------------+ | Output                | +-----------------------+ | Your Square Value=225 | +-----------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)

Fix: ERROR 1396 (HY000): Operation CREATE USER failed in MySQL?

AmitDiwan
Updated on 30-Dec-2019 07:03:08

2K+ Views

To fix the error, let us see how to create a user correctly. Let us create a user −mysql> create user 'Emma'@'localhost' IDENTIFIED BY 'emma_654'; Query OK, 0 rows affected (0.00 sec)Let us display all users along with host −mysql> select user, host from MySQL.user;This will produce the following output. The new user created above is visible in the below list of all users along with host −+------------------+-----------+ | user             |      host | +------------------+-----------+ | Bob              |         % | | Charlie   ... Read More

Query the database for the values not in the MySQL table?

AmitDiwan
Updated on 30-Dec-2019 06:57:24

55 Views

For this, you can use UNION ALL along with WHERE NOT EXISTS and implement NOT IN to ignore the values already in the table. Use SELECT with UNION ALL to add values not already in the table.Let us first create a table −mysql> create table DemoTable1918    (    Value int NOT NULL AUTO_INCREMENT PRIMARY KEY    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1918 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1918 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Get all rows apart from first and last in MySQL

AmitDiwan
Updated on 30-Dec-2019 06:55:04

111 Views

To get all rows apart from first and last, use subquery along with MIN() and MAX(). Let us first create a table −mysql> create table DemoTable1917    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentCode int,    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1917(StudentCode, StudentMarks) values(78, 95); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode, StudentMarks) values(78, 96); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode, StudentMarks) values(78, 97); Query OK, 1 row affected (0.00 ... Read More

Write MySQL case statement to set custom messages for student’s result

AmitDiwan
Updated on 30-Dec-2019 06:51:31

125 Views

For this, set conditions using MySQL CASE statement −mysql> create table DemoTable1916    (    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1916 values('Chris', 59); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('David', 89); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Sam', 94); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Mike', 75); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Carol', 69); Query OK, 1 row affected ... Read More

Set 1 for NOT NULL value in MySQL

AmitDiwan
Updated on 30-Dec-2019 06:44:17

201 Views

To set NOT NULL, use IS NOT NULL and find the value. The syntax is as follows −select if('' is not NULL, 1, 0) as anyAliasName;Here is the working query −mysql> select if('' is not NULL, 1, 0);This will produce the following output −+------------------------+ | if('' is not NULL, 1, 0) | +------------------------+ |                      1 | +------------------------+ 1 row in set (0.00 sec)To understand the above syntax, let us create a table −mysql> create table DemoTable1915    (    Name varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert ... Read More

MySQL: How can I find a value with special character and replace with NULL?

AmitDiwan
Updated on 30-Dec-2019 06:42:52

196 Views

For this, use SET yourColumnName = NULL as in the below syntax −update yourTableName set yourColumnName=NULL where yourColumnName=yourValue;Let us first create a table −mysql> create table DemoTable1914    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Code varchar(20)    )AUTO_INCREMENT=1001; Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1914(Code) values('John101'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1914(Code) values('234David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1914(Code) values('100_Mike'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select ... Read More

Advertisements