我正在尝试在我的数据库上执行此查询,该数据库根据第三个表的结果查询2个表。
SELECT *
FROM ads_user AS u
INNER JOIN ads_medium AS m
ON u.id = m.owner_id
WHERE m.id IN (SELECT medium_id,
Count(*) AS count
FROM ads_click
WHERE time > '2017-01-01'
GROUP BY medium_id
ORDER BY count DESC
LIMIT 100);
正如您所看到的,我在子查询中使用了两列,count
列是整个查询工作所必需的。不幸的是PostgreSql抛出subquery has too many columns
错误。
有没有解决方法呢?
答案 0 :(得分:1)
只需将计数作为选定列删除,然后将(*)添加到订单中。因此:
SELECT *
FROM ads_user AS u
INNER JOIN ads_medium AS m
ON u.id = m.owner_id
WHERE m.id IN (SELECT medium_id
FROM ads_click
WHERE time > '2017-01-01'
GROUP BY medium_id
ORDER BY count(*) DESC
LIMIT 100);
Postgres抱怨是因为你的IN引用了两列,而它应该只有一列。将Count(*)按顺序排列是完全可以的。