简体   繁体   English

仅返回一个SQL查询结果元组

[英]Return only one tuple of SQL query result

I want to do a query like 我想做一个查询

select * from chr2;

but only have MySQL return the first tuple (or an arbitrary) tuple instead of all of them. 但只有MySQL返回第一个元组(或任意)元组而不是所有元组。

How do I do it? 我该怎么做?

Use the LIMIT clause: 使用LIMIT子句:

SELECT * FROM chr2 LIMIT 1;

If you want an arbitrary row returned, you have to sort your rows by an random col like this ( MySQL docu ): 如果要返回任意行,则必须按如下所示的随机col( MySQL docu )对行进行排序:

SELECT * FROM chr2 
ORDER BY RAND()
LIMIT 1;

On large tables, however, you might run into performance problems with this, as there a random value has to be created for each row and the table has to be sorted according to this column. 但是,在大型表上,您可能会遇到性能问题,因为必须为每行创建一个随机值,并且必须根据此列对表进行排序。

尝试这个 ::

select * from chr2 limit 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM