Found 4219 Articles for MySQLi

MySQL group_concat to add a separator for empty fields?

AmitDiwan
Updated on 26-Sep-2019 07:33:08

342 Views

For this, you can use replace() along with group_concat(). Here, for empty fields, we are displaying a comma as a separator. Let us first create a table −mysql> create table DemoTable (    Id int,    Number varchar(100) ); Query OK, 0 rows affected (2.03 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, '456'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(11, '345'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(10, ''); Query OK, 1 row affected (0.63 sec) mysql> insert into DemoTable values(10, '278'); Query ... Read More

How to ORDER BY DESC and display the first 3 records in MySQL?

AmitDiwan
Updated on 26-Sep-2019 07:29:43

494 Views

For this, you can use ORDER BY DESC with LIMIT. Let us first create a table −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(100) ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(UserName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(UserName) values('Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(UserName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(UserName) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(UserName) ... Read More

MySQL ORDER BY strings with underscore?

AmitDiwan
Updated on 26-Sep-2019 07:27:27

233 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John_Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('John_Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select ... Read More

Insert multiple rows from another table but the inserted records should be distinct

AmitDiwan
Updated on 26-Sep-2019 07:24:07

215 Views

For this, you can use DISTINCT along with the INSERT INTO SELECT statement. Let us first create a table −mysql> create table DemoTable1 (    Value int ); Query OK, 0 rows affected (1.03 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(50); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values(60); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1 values(50); Query OK, 1 row ... Read More

Limit the count using GROUP BY in MySQL

AmitDiwan
Updated on 26-Sep-2019 07:20:43

1K+ Views

Let us first create a table −mysql> create table DemoTable (    UserId int,    UserMessage varchar(100) ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 'Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(2, 'Hello'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(2, 'Good'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(1, 'Nice'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(1, 'Awesome'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

How to get MySQL query result in same order as given by IN clause?

AmitDiwan
Updated on 26-Sep-2019 07:18:18

1K+ Views

For this, you can use IN() along with ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values('Sam'); Query OK, 1 row affected (0.12 sec) mysql> insert into ... Read More

How to display all the MySQL tables in one line?

AmitDiwan
Updated on 26-Sep-2019 07:15:49

394 Views

Use information_schema.tables to display all the tables. With that, se the database name as well, so that you can display tables only from a specific database.Let us now display all the tables in the database “web” −mysql> select group_concat(table_name) from information_schema.tables where table_schema='web';This will produce the following output −+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | group_concat(table_name) | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | demo_table725, demotabe619, demotabe620, demotable211, demotable212, demotable213, demotable214, demotable215, demotable216, demotable217, demotable218, demotable219, demotable220, demotable221, demotable222, demotable223, demotable224, demotable225, demotable226, demotable227, demotable228, demotable229, demotable230, demotable231, demotable232, demotable233, demotable234, demotable235, demotable236, demotable237, demotable238, demotable239, demotable240, demotable241, demotable244, demotable245, demotable246, demotable247, demotable248, demotable249, demotable250, demotable251, demotable252, demotable 253, demotable254, ... Read More

How to select all the characters after the first 20 characters from a column in MySQL?

AmitDiwan
Updated on 26-Sep-2019 07:11:59

171 Views

Let us first create a table −mysql> create table DemoTable (    Title text ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('C is a good programming language to start'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Java is good with data structure and algorithm'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Coding is very important'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

MySQL query to find single value from duplicates with certain condition by excluding other records using NOT IN

AmitDiwan
Updated on 26-Sep-2019 07:08:35

56 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(100) ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100, 'Robert'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable values(100, 'Mike'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(100, 'Sam'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable values(101, 'David'); Query OK, 1 row affected (0.19 sec) mysql> insert ... Read More

Underscore as a table name in MySQL is possible?

AmitDiwan
Updated on 26-Sep-2019 07:05:44

183 Views

Yes, we can add underscore as a table name using backticks around the table name. Following is the syntax −INSERT INTO `yourTableName` values(yourValue1, .......N);Let us first create a table −mysql> create table `DemoTable_1` (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Location varchar(100) ); Query OK, 0 rows affected (1.17 sec)Insert some records in the table using insert command −mysql> insert into `DemoTable_1`(Location) values('C:/myFolder/JavaFiles'); Query OK, 1 row affected (0.20 sec) mysql> insert into `DemoTable_1`(Location) values('E:/AllJarOfJava'); Query OK, 1 row affected (0.17 sec) mysql> insert into `DemoTable_1`(Location) values('C:/ProgramFiles'); Query OK, 1 row affected (0.17 sec)Display all records from ... Read More

Advertisements