[英]creating a custom column from joining two tables
如果那是我需要做的,我對子查詢很糟糕。 首先,讓我向您展示我的表格的預覽以及我正在嘗試做的事情。
這是我最后想要的結果:
business.name
reviews_count (total count, matching the current queries business_id)
where the b.industry_id matches 7
這是我正在嘗試的方法,但是我感到很困惑,不知道如何匹配總數,請讓我解釋一下:
select
b.name,
reviews_count as (select count(*) as count from reviews where business_id = b.business_id),
from business as b
left join reviews as r
on r.business_id = b.id
where b.industry_id = 7
子查詢business_id需要與正在運行的當前企業ID相匹配。 希望我有道理。 (reviews_count不存在,我只是在輸出時才使用它)
這看起來像是GROUP BY
的工作
SELECT
b.name,
count(distinct r.id)
FROM
businesses b
JOIN reviews r ON r.business_id = b.id
WHERE b.industry_id = 7
GROUP BY b.id
這樣,您可以完全避免子查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.