Home > Database > Mysql Tutorial > body text

Does it make sense to use 'LIMIT 1' in the query 'SELECT 1 ...'?

王林
Release: 2023-09-03 08:21:08
forward
1143 people have browsed it

在查询“SELECT 1 ...”中使用“LIMIT 1”是否有意义?

Yes, you can use LIMIT 1 in SELECT 1.

Suppose you are using SELECT 1 and your table has billions of records. In this case it prints 100 million times.

The syntax of SELECT 1 is as follows −

SELECT 1 FROM yourTableName;
Copy after login

Suppose, you are using LIMIT 1 and your table has billions of records. This case, it will print 1 only once.

The syntax of SELECT 1 with LIMIT 1 is as follows −

SELECT 1 FROM yourTableName LIMIT 1;
Copy after login

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table Select1AndLimit1Demo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (1.99 sec)
Copy after login

Use insert command to insert some records in the table. The query is as follows −

mysql> insert into Select1AndLimit1Demo(Name) values('John');
Query OK, 1 row affected (0.21 sec)
mysql> insert into Select1AndLimit1Demo(Name) values('Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into Select1AndLimit1Demo(Name) values('Sam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into Select1AndLimit1Demo(Name) values('Bob');
Query OK, 1 row affected (0.18 sec)
mysql> insert into Select1AndLimit1Demo(Name) values('David');
Query OK, 1 row affected (0.14 sec)
mysql> insert into Select1AndLimit1Demo(Name) values('Mike');
Query OK, 1 row affected (0.20 sec)
mysql> insert into Select1AndLimit1Demo(Name) values('Maxwell');
Query OK, 1 row affected (0.11 sec)
Copy after login

Display all records from the table using a select statement. The query is as follows −

mysql> select *from Select1AndLimit1Demo;
Copy after login

Output

+----+---------+
| Id | Name    |
+----+---------+
|  1 | John    |
|  2 | Carol   |
|  3 | Sam     |
|  4 | Bob     |
|  5 | David   |
|  6 | Mike    |
|  7 | Maxwell |
+----+---------+
7 rows in set (0.00 sec)
Copy after login

Above, we have a table with 7 records. Therefore, the output is 7 times 1.

Let us now see the case of SELECT 1 with LIMIT 1. The query is as follows −

mysql> select 1 from Select1AndLimit1Demo;
Copy after login

The following is the output result, only showing the value 1 once −

+---+
| 1 |
+---+
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
+---+
7 rows in set (0.00 sec)
Copy after login

Above, our table has 7 records. We get 1 times 1 because we used LIMIT 1.

The above is the detailed content of Does it make sense to use 'LIMIT 1' in the query 'SELECT 1 ...'?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template