如何进行查询

ecr0jaav  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(305)

审阅表有store\u idx、user\u idx等。。。
我想创建一个查询语句,该语句获取有关用户用输入的userid值作为书签的商店的信息。
我的疑问句是

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 = ?)

然而,结果什么也没有出来。
帮助我。。

0ve6wy6x

0ve6wy6x1#

请使用以下查询:

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
uyhoqukh

uyhoqukh2#

这个 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 = ?
                     );

相关问题