Found 4381 Articles for MySQL

How to select rows if initial ones are randomized and the rest ordered by criteria with MySQL?

AmitDiwan
Updated on 30-Dec-2019 07:28:20

115 Views

For this, you can use ORDER BY CASE statement. Let us create a table −mysql> create table DemoTable1926    (    Position varchar(20),    Number int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1926 values('Highest', 50); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1926 values('Highest', 30); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1926 values('Lowest', 100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1926 values('Lowest', 120); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1926 values('Lowest', ... Read More

How to update a specific column value fetched with CASE statement?

AmitDiwan
Updated on 30-Dec-2019 07:22:10

135 Views

For this, use UPDATE command along with CASE statement. Let us first create a table −mysql> create table DemoTable1925    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    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 DemoTable1925(StudentName, StudentMarks) values('Chris', 98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1925(StudentName, StudentMarks) values('David', 45); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1925;This will produce the following output −+-----------+-------------+--------------+ | ... Read More

Update the records in a table with a specific year fetched from date format like '10/12/2010'?

AmitDiwan
Updated on 30-Dec-2019 07:20:19

143 Views

To update records with a specific year, use the YEAR() method as in the below syntax:update yourTableName set yourColumnName1=yourValue1 where YEAR(str_to_date(yourColumnName2, '%d/%m/%Y'))=yourValue2;Let us first create a table −mysql> create table DemoTable1924    (    UserName varchar(20),    UserJoiningDate varchar(40)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1924 values('Chris', '10/12/2010'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1924 values('David', '20/01/2011'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1924 values('Mike', '20/01/2010'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1924 values('Carol', ... Read More

MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT

AmitDiwan
Updated on 30-Dec-2019 07:17:37

1K+ Views

Let us create a table −mysql> create table DemoTable1923    (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1923(UserId, UserName)      select 101 as UserId, 'Chris' as UserName; Query OK, 1 row affected (0.00 sec) Records: 1  Duplicates: 0  Warnings: 0 mysql> insert into DemoTable1923(UserId, UserName)     select 102 as UserId, 'Robert' as UserName; Query OK, 1 row affected (0.00 sec) Records: 1  Duplicates: 0  Warnings: 0 mysql> insert into DemoTable1923(UserId, UserName) ... Read More

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

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

220 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

169 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

10K+ 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

551 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

3K+ 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

120 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

Advertisements