如何在子查询中有 2 列但忽略其中一列?
How to have 2 columns in a subquery but ignore one of them?
我正在尝试在我的数据库上执行此查询,该数据库正在根据第三个 table.
的结果查询 2 tables
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
错误。
有什么解决方法吗?
只需删除作为选定列的计数并在排序依据中添加 (*)。因此:
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 引用了两列,而它应该只有一列。在 order by 中使用 Count(*) 是完全可以的。
我正在尝试在我的数据库上执行此查询,该数据库正在根据第三个 table.
的结果查询 2 tablesSELECT *
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
错误。
有什么解决方法吗?
只需删除作为选定列的计数并在排序依据中添加 (*)。因此:
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 引用了两列,而它应该只有一列。在 order by 中使用 Count(*) 是完全可以的。