我有两个表,一个包含一个watch_list
用一些重要属性调用的项目列表,另一个只是一个名为price_history
. 我想要做的是使用 group_concat 操作将 10 个最低价格组合到一个列中,然后创建一个包含项目属性的行watch_list
以及watch_list
. 首先,我尝试了连接,但后来我意识到这些操作以错误的顺序发生,因此我无法通过连接操作获得所需的结果。然后我尝试了显而易见的事情,只是查询price_history
了watch_list
并且只是将主机环境中的所有内容粘合在一起,该环境有效但似乎效率很低。现在我有以下查询,它看起来应该可以工作,但它没有给我想要的结果。我想知道以下语句有什么问题:
select w.asin,w.title,
(select group_concat(lowest_used_price) from price_history as p
where p.asin=w.asin limit 10)
as lowest_used
from watch_list as w
Run Code Online (Sandbox Code Playgroud)
基本上我希望该limit
操作在group_concat
做任何事情之前发生,但我想不出一个能做到这一点的 sql 语句。
想通了,正如有人曾经说过的“计算机科学中的所有问题都可以通过另一个间接级别来解决”。在这种情况下,一个额外的select
子查询起到了作用:
select w.asin,w.title,
(select group_concat(lowest_used_price)
from (select lowest_used_price from price_history as p
where p.asin=w.asin limit 10)) as lowest_used
from watch_list as w
Run Code Online (Sandbox Code Playgroud)