是的,你可以在SELECT 1中使用LIMIT 1。
假設(shè)你正在使用SELECT 1,而你的表有數(shù)十億條記錄。在這種情況下,它會(huì)打印1億次。
SELECT 1的語法如下所示 ?
SELECT 1 FROM yourTableName;
登錄后復(fù)制
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;
登錄后復(fù)制
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)
登錄后復(fù)制
使用插入命令在表中插入一些記錄。查詢?nèi)缦?−
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)
登錄后復(fù)制
Display all records from the table using a select statement. The query is as follows −
mysql> select *from Select1AndLimit1Demo;
登錄后復(fù)制
輸出
+----+---------+ | Id | Name | +----+---------+ | 1 | John | | 2 | Carol | | 3 | Sam | | 4 | Bob | | 5 | David | | 6 | Mike | | 7 | Maxwell | +----+---------+ 7 rows in set (0.00 sec)
登錄后復(fù)制
這是SELECT 1的案例。查詢?nèi)缦?−
mysql> select 1 from Select1AndLimit1Demo;
登錄后復(fù)制
輸出
+---+ | 1 | +---+ | 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 1 | +---+ 7 rows in set (0.00 sec)
登錄后復(fù)制
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 limit 1;
登錄后復(fù)制
以下是輸出的結(jié)果,只顯示值為1一次 −
+---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.00 sec)
登錄后復(fù)制
上面,我們的表有7條記錄。我們得到了1乘以1,因?yàn)槲覀兪褂昧薒IMIT 1。
以上就是在查詢“SELECT 1 …”中使用“LIMIT 1”是否有意義?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!