SQL 如何在不使用 rowcount 的情况下仅选择 sybase 中的 1 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4766054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select only 1 row in sybase without using rowcount
提问by user433500
How do I select only 1 row in sybase without using rowcount
? I don't have the privilege to set rowcount
in sybase. Is there a way to select only 1 row?
如何在不使用的情况下仅选择 sybase 中的 1 行rowcount
?我没有set rowcount
在 sybase 中的特权。有没有办法只选择1行?
For example:
例如:
select * from table where name = 'Hyman'
This returns two rows; how do I select only one row from the result set without using set rowcount
?
这将返回两行;如何在不使用的情况下只从结果集中选择一行set rowcount
?
回答by Peach
Try the query:
试试查询:
SELECT TOP 1 * FROM mytable
WHERE name = 'Hyman'
As you might guess, this selects the TOP 1 matching results. If you wanted more (which you don't here) you could use any number (TOP 100
or TOP 1000
, etc).
您可能会猜到,这会选择 TOP 1 匹配结果。如果您想要更多(您在这里没有),您可以使用任何数字(TOP 100
或TOP 1000
等)。
A more comprehensive example can be found on w3schools: http://www.w3schools.com/Sql/sql_top.asp
可以在 w3schools 上找到更全面的示例:http: //www.w3schools.com/Sql/sql_top.asp
回答by B0rG
There seems to be a reason, why you're getting more than 1 row for "WHERE name = 'Hyman'", it looks as if the rows differ.
似乎有一个原因,为什么您会为“WHERE name = 'Hyman'”获得超过 1 行,看起来行不同。
But if, the rows do not differ you can try adding "distinct":
但是,如果行没有不同,您可以尝试添加“distinct”:
SELECT DISTINCT * FROM TABLE WHERE name = 'Hyman'
or try with "GROUP BY" statement, then you should type explicitly all columns, eg.:
或尝试使用“GROUP BY”语句,然后您应该明确输入所有列,例如:
SELECT name FROM TABLE WHERE name = 'Hyman' GROUP BY name
if this is not what you wanted, can you paste here how the 2 rows look exactly?
如果这不是您想要的,您能否在此处粘贴 2 行的外观?
回答by Rick
If you want a single result, use 'GROUP BY' and 'HAVING column = max(column)'. Or replace max() with min(). This should work unless the max or min values are also not unique.
如果您想要单个结果,请使用“GROUP BY”和“HAVING column = max(column)”。或者用 min() 替换 max()。除非最大值或最小值也不是唯一的,否则这应该有效。