【问题标题】:Select a particular row in when there are multiple options当有多个选项时选择特定行
【发布时间】:2014-12-01 16:24:49
【问题描述】:

这是一个从表中选择行的查询。该表可能存在许多具有特定 (WritingSystem,InterpretationId) 配对的行。

select Form,WritingSystem,_id from TextForms where InterpretationId=?;

我希望结果集(最多)为每个 WritingSystem 包含一行,并且我希望它是长度(Form)的最小值的行。如果没有某种丑陋的连接,我无法弄清楚如何做到这一点。

这个查询当然只返回一行,而我想要多行,其中有多个 WritingSystem 值。

select Form,WritingSystem,_id from TextForms where InterpretationId=? limit 1;

这个查询正确地为每个 WritingSystem 的值返回一行,但就我所见,选择哪一行是任意的。

select Form,WritingSystem,_id from TextForms where InterpretationId=? group by WritingSystem;

【问题讨论】:

  • 您有可以发布的示例数据吗?

标签: sqlite select


【解决方案1】:

当您使用 GROUP BY 时,您必须指定如何为每个组计算输出值。这通常使用 MIN/MAX/COUNT/SUM 等聚合函数来完成。

在 SQLite 3.7.11 及更高版本中,当您使用 MIN/MAX 时,_id 等其他列保证来自与 MIN 匹配的同一行:

SELECT Form, WritingSystem, _id, MIN(LENGTH(Form))
FROM TextForms
WHERE InterpretationId = ?
GROUP BY WritingSystem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2015-08-15
    • 1970-01-01
    • 2016-09-18
    • 2019-03-30
    相关资源
    最近更新 更多