Found 4219 Articles for MySQLi

How to remove Duplicate Records except a single record in MySQL?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

You can use DELETE command with some condition for this since we need to keep one record and delete rest of the duplicate records.Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(40) ); Query OK, 0 rows affected (0.48 sec)Insert records in the table using insert command −mysql> insert into DemoTable(StudentName) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentName) values('Sam'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(StudentName) ... Read More

How to select from a set of integers in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

180 Views

To select from a set of integers, you can use UNION. Following is the syntax −SELECT yourValue1 UNION SELECT yourValue2 UNION SELECT yourValue3 UNION SELECT yourValue4 . . . . NLet us implement the above syntax to select from a set of integers in MySQL −mysql> SELECT 1000 UNION SELECT 2000 UNION SELECT 3000 UNION SELECT 4000 UNION SELECT 5000 UNION SELECT 6000 UNION SELECT 7000;This will produce the following output −+------+ | 1000 | +------+ | 1000 | | 2000 | | 3000 | | 4000 | | 5000 | | 6000 | | 7000 | +------+ 7 rows in set (0.03 sec)

Replace dot with comma on MySQL SELECT?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

5K+ Views

To replace dot with comma on SELECT, you can use REPLACE(). Following is the syntax −select replace(yourColumnName, '.' , ', ' ) from yourTableName;Let us first create a table −mysql> create table DemoTable (    Value float ); Query OK, 0 rows affected (0.63 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(4.56); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(456.23); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(1078.3); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(2000.50); Query OK, 1 row affected (0.26 sec) mysql> ... Read More

Which datatype is should I use to set a column for 5-star rating?

Samual Sam
Updated on 30-Jul-2019 22:30:25

537 Views

You can use ENUM datatype for this. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserRating ENUM('1', '2', '3', '4', '5') ); Query OK, 0 rows affected (0.54 sec)Insert records in the table using insert command −mysql> insert into DemoTable(UserRating) values('5'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(UserRating) values('3'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserRating) values('1'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(UserRating) values('3'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(UserRating) values('4'); ... Read More

MySQL query to select too many rows?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

406 Views

You can use LIMIT for this, which is used to fetch limited number of records. Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(20) ); Query OK, 0 rows affected (0.53 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(11, 'Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(12, 'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(13, 'Carol'); Query OK, 1 row affected (0.13 sec) mysql> ... Read More

MySQL query to perform delete operation where id is the biggest?

Samual Sam
Updated on 30-Jul-2019 22:30:25

141 Views

You can use ORDER BY DESC command with LIMIT 1 for this since we need to delete only a single ID.Let us first create a table −mysql> create table DemoTable (    UserId int,    UserName varchar(20) ); Query OK, 0 rows affected (0.57 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(100, 'John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(234, 'Mike'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(145, 'Sam'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(278, 'Carol'); Query OK, 1 ... Read More

How to display message from a stored procedure?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

5K+ Views

To display message from stored procedure on the basis of conditions, let us use IF-ELSE condition −mysql> DELIMITER // mysql> CREATE PROCEDURE showMessage(value int, Name varchar(20))    BEGIN       IF(value > 100) then          SELECT CONCAT("HELLO", " ", Name);       ELSE          SELECT CONCAT("BYE", " ", Name);       END IF;       END       // Query OK, 0 rows affected (0.18 sec) mysql> DELIMITER ;Case 1 − Call the stored procedure using CALL command, when value is more than 100 −call showMessage(200, 'John');This will produce ... Read More

How to check if data is NULL in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

332 Views

You can use IF() to check if data is NULL.  Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(200),    Age int ); Query OK, 0 rows affected (0.44 sec)Insert records in the table using insert command −mysql> insert into DemoTable(Name, Age) values('John', 23); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name, Age) values('Sam', null); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name, Age) values('Mike', 23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Name, Age) values('David', 21); Query OK, ... Read More

Select first word in a MySQL query?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

4K+ Views

To select first word in MySQL query, you can use SUBSTRING_INDEX(). Following is the syntax −select substring_index(yourColumnName, ' ', 1) as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFullName varchar(40) ); Query OK, 0 rows affected (0.61 sec)Insert records in the table using insert command −mysql> insert into DemoTable(StudentFullName) values('John Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFullName) values('Carol Taylor'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentFullName) values('Bob Williams'); Query OK, 1 row affected (0.13 sec) mysql> insert ... Read More

Selecting a column that is also a keyword in MySQL?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

7K+ Views

To select a column that is also a keyword in MySQL, you need to use backticks around the column name. As you know select is a keyword in MySQL, consider column name as select when creating a new table.Let us create a table:mysql> create table DemoTable (`select` varchar(100)); Query OK, 0 rows affected (0.53 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values('Records'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('All Data'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Information'); Query OK, 1 ... Read More

Advertisements