评论表有store_idx,user_idx等...
我想创建一个查询语句,通过输入的user_id值获取用户已加入书签的商店信息。
我所做的查询句子是
select A.store_name
, A.store_img
, count(B.store_idx) as review_cnt
from board.store A
Left
Join board.review B
On A.store_idx is B.store_idx
where store_idx is (select A.store_idx from bookmark where user_id = ?)
然而,结果没有出现。
帮帮我......
答案 0 :(得分:0)
请使用以下查询:
SELECT store_name
, store_img
, SUM(review_cnt) AS review_cnt
FROM
( SELECT DISTINCT A.store_name
, A.store_img
, CASE WHEN B.store_idx IS NULL THEN 0 ELSE 1 END AS review_cnt
FROM bookmark br
JOIN board.store A
ON A.store_idx = br.store_idx
LEFT
JOIN board.review B
ON A.store_idx = B.store_idx
WHERE br.user_id = ?
)T
答案 1 :(得分:0)
WHERE
子句显然是过滤掉所有行。我们对此无能为力。但是您的查询也缺少GROUP BY
,表别名可以改进,join
条件不正确。
所以,试试这个版本:
select s.store_name, s.store_img, count(b.store_idx) as review_cnt
from board.store s left join
board.review r
on s.store_idx = r.store_idx
where b.store_idx in (select b.store_idx
from bookmark b
where b.user_id = ?
);