Found 4219 Articles for MySQLi

Display first non-null values with coalesce() in MySQL?

AmitDiwan
Updated on 30-Dec-2019 07:31:04

367 Views

The coalesce() can be used to print first NOT NULL column value. Let us first create a table −mysql> create table DemoTable1927    (    StudentName varchar(20),    StudentSubject varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1927 values('Chris', 'MySQL'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1927 values('David', NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1927 values(NULL, 'MongoDB'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1927;This will ... Read More

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

53 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

87 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

85 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

823 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

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

Advertisements