在查詢“SELECT 1 …”中使用“LIMIT 1”有意義嗎?
是的,你可以將 LIMIT 1 與 SELECT 1 一起使用。
假設你使用 SELECT 1,並且你的表有數十億條記錄。在這種情況下,它將列印數十億次 1。
SELECT 1 的語法如下:
SELECT 1 FROM yourTableName;
假設你使用 LIMIT 1,並且你的表有數十億條記錄。在這種情況下,它只會列印一次 1。
帶有 LIMIT 1 的 SELECT 1 語法如下:
SELECT 1 FROM yourTableName LIMIT 1;
為了理解上述語法,讓我們建立一個表。建立表的查詢如下:
mysql> create table Select1AndLimit1Demo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (1.99 sec)
使用 insert 命令在表中插入一些記錄。查詢如下:
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)
使用 select 語句顯示錶中的所有記錄。查詢如下:
mysql> select *from Select1AndLimit1Demo;
輸出
+----+---------+ | Id | Name | +----+---------+ | 1 | John | | 2 | Carol | | 3 | Sam | | 4 | Bob | | 5 | David | | 6 | Mike | | 7 | Maxwell | +----+---------+ 7 rows in set (0.00 sec)
這是 SELECT 1 的情況。查詢如下:
mysql> select 1 from Select1AndLimit1Demo;
輸出
+---+ | 1 | +---+ | 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 1 | +---+ 7 rows in set (0.00 sec)
上面,我們有一個包含 7 條記錄的表。因此,輸出是 7 次 1。
現在讓我們看看帶有 LIMIT 1 的 SELECT 1 的情況。查詢如下:
mysql> select 1 from Select1AndLimit1Demo limit 1;
以下是僅顯示一次值 1 的輸出:
+---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.00 sec)
上面,我們的表有 7 條記錄。因為我們使用了 LIMIT 1,所以我們得到 1 次 1。
廣告