在SQLITE中的相关子查询中没有这样的列错误(是:如何选择每组的前N个元素)

时间:2017-05-22 18:21:00

标签: sql sqlite correlated-subquery

我在PHP服务器上执行以下查询

SELECT vv.id, vv.feature_id, vv.val, vv.lab, vv.variable_id, vv.subset_id, us.ord, us.nam subset_nam, us.ord, us.color
FROM variable_values vv
  LEFT JOIN unit_subsets us ON vv.subset_id = us.id
  INNER JOIN variables v ON vv.variable_id = v.id
WHERE v.nam='LanguageDistribution'
      AND vv.feature_id IN (37,40...)

并返回65000 rowws,耗尽可用内存。

稍后,我通过vv.feature_id以编程方式对行进行分组,并且每个组仅保留N个最大值。

我可以在SQL端执行此操作吗?

写作时

SELECT vv.id, vv.feature_id, vv.val, vv.lab, vv.variable_id, vv.subset_id, us.ord, us.nam subset_nam, us.ord, us.color
FROM variable_values vv
  LEFT JOIN unit_subsets us ON vv.subset_id = us.id
  INNER JOIN variables v ON vv.variable_id = v.id
WHERE v.nam='LanguageDistribution' AND
    vv.id IN (SELECT vv2.id FROM variable_values vv2 WHERE vv2.variable_id = v.id ORDER BY vv2.val DESC LIMIT 5)
      AND vv.feature_id IN (37,40,63...

它发誓

  

[SQLITE_ERROR] SQL错误或缺少数据库(没有这样的列:v.id)

如何成功?

0 个答案:

没有答案