【问题标题】:Mysql search for specific on criteriasMysql搜索特定条件
【发布时间】:2018-02-15 04:34:16
【问题描述】:

我正在 rstudio 中编写 mysql 代码,我想找到一行显示最小 weight_g(以克为单位的重量)。然后我使用下面的命令,但它显示错误。

Q("select * from bl where Weight_g = min(Weight_g)") [1]《HY000 1111 [MySQL][ODBC 5.3(w)驱动程序][mysqld-5.7.17-log]组函数使用无效"
[2] "[RODBC] 错误:无法 SQLExecDirect 'select * from bl where Weight_g = min(Weight_g)'"

我被这个错误信息困住了,非常感谢任何帮助!谢谢!

【问题讨论】:

  • 在 "in (select min(Weight_g) from bl) 中更改 "= min(Weight_g)"

标签: mysql


【解决方案1】:

您可以在子查询中获取最小值,

select * 
from bl 
where Weight_g = (SELECT min(Weight_g) from bl)

如果您不关心重复并且只想获得一个值,那么ORDER BYLIMIT 就足够了,

select * 
from bl 
ORDER BY Weight_g ASC
LIMIT 1

【讨论】:

  • 第二个查询有语法错误,去掉where子句。
【解决方案2】:

试试这个,不要重复:

select * 
from bl 
ORDER BY Weight_g ASC
LIMIT 1;

或者这些以获得具有最小权重的所有不同实例:

select A.*
from bl A
where 
A.weight_g=(select weight_g from bl ORDER BY weight_g ASC
LIMIT 1);

或者:

select A.*
from bl A
where 
A.weight_g=(select min(weight_g) from bl);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2011-08-20
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多