Found 4219 Articles for MySQLi

Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL

AmitDiwan
Updated on 27-Sep-2019 07:19:12

60 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100) ); Query OK, 0 rows affected (0.96 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | FirstName | ... Read More

How can I count unique records from a column in MySQL database?

AmitDiwan
Updated on 27-Sep-2019 07:17:32

123 Views

For this, use aggregate function count(*) to count to GROUP BY to group. Let us first create a table −mysql> create table DemoTable (    UserName varchar(100),    UserPostMessage text ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 'Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('David', 'Hello'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris', 'Awesome'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris', 'Amazing'); Query OK, 1 row affected (0.17 sec) mysql> insert ... Read More

MySQL query to display records ordered by DESC while skipping some?

AmitDiwan
Updated on 27-Sep-2019 07:14:25

60 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(1, 'David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(4, 'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(6, 'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(7, 'Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

Find the next lowest number higher than a certain number in MySQL?

AmitDiwan
Updated on 27-Sep-2019 07:11:28

215 Views

For this, use aggregate function MIN() along with WHERE clause. Let us first create a table −mysql> create table DemoTable (    Number int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(11); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This ... Read More

MySQL query to change a string by displaying only the part of string after underscore?

AmitDiwan
Updated on 27-Sep-2019 07:04:32

68 Views

Let us first create a table −mysql> create table DemoTable (    UserName varchar(100) ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Smith_John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Smith_Adam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Smith_David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Smith_Mike'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------------+ | UserName ... Read More

MySQL query to find expiry date (record) from the next 2 days?

AmitDiwan
Updated on 27-Sep-2019 07:01:58

1K+ Views

For this, you can use BETWEEN keyword. Let us first create a table −mysql> create table DemoTable (    ExpiryDate date ); Query OK, 0 rows affected (0.55 sec)Note − Let’s say the current date is 2019-08-18.Insert some records in the table using insert command −mysql> insert into DemoTable values('2018-01-21'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-08-20'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values('2018-08-20'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('2019-08-21'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select ... Read More

MySQL query to find the average of rows with the same ID

AmitDiwan
Updated on 27-Sep-2019 06:58:20

457 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentMarks int ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1000, 78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(1001, 88); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(1000, 89); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(1000, 67); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1000, 90); Query OK, 1 row affected (0.21 sec) mysql> insert ... Read More

How can I create a MySQL boolean column and assign value 1 while altering the same column?

AmitDiwan
Updated on 27-Sep-2019 06:55:48

167 Views

To assign value 1 while altering, use the MySQL DEFAULT. This will itself enter 1 if nothing is inserted in the same column while using the INSERT command.Let us first create a table −mysql> create table DemoTable (    isAdult int ); Query OK, 0 rows affected (1.39 sec)Following is how you can assign value 1 default to the already created column −mysql> alter table DemoTable CHANGE isAdult isAdult BOOLEAN DEFAULT '1' NOT NULL; Query OK, 0 rows affected (1.18 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable values(); ... Read More

How to find the minimum and maximum values in a single MySQL Query?

AmitDiwan
Updated on 27-Sep-2019 06:53:29

307 Views

To find the minimum and maximum values in a single query, use MySQL UNION. Let us first create a table −mysql> create table DemoTable (    Price int ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement ... Read More

MySQL query to find the number of rows in the last query

AmitDiwan
Updated on 27-Sep-2019 06:51:16

164 Views

For this, use the FOUND_ROWS in MySQL. Following is the syntax −SELECT SQL_CALC_FOUND_ROWS TABLE_NAME FROM `information_schema`.tables WHERE TABLE_NAME LIKE "yourValue%" LIMIT yourLimitValue;Here, I am using the database ‘web’ and I have lots of tables, let’s say which begins from DemoTable29. Let us implement the above syntax to fetch only 4 of such rows −mysql> SELECT SQL_CALC_FOUND_ROWS TABLE_NAME FROM `information_schema`.tables WHERE TABLE_NAME LIKE "DemoTable29%" LIMIT 4;This will produce the following output −+--------------+ | TABLE_NAME | +--------------+ | demotable29 | | demotable290 | | demotable291 | | demotable292 | +--------------+ 4 rows in set (0.01 sec)Here is the query ... Read More

Advertisements