日期:2014-05-16  浏览次数:20948 次

这个语句怎么用关联的方法写出来。还可以怎么改能实现和这个语句一样的查询结果。
  select a.dish_name, nvl(comments.sl, 0) pinglun
              
                from (select dish_name, dish_id from jw_dish) a
              
                left join (select a.dish_id, count(b.comment_heart) sl
                             from jw_order_arch a, jw_comment b
                            where b.single_order_id = a.single_order_id
                            group by a.dish_id) comments
                  on comments.dish_id = a.dish_id
------解决方案--------------------
引用:
  select a.dish_name, nvl(comments.sl, 0) pinglun
              
                from (select dish_name, dish_id from jw_dish) a
              
                left join (select a.dish_id, count(b.comment_heart) sl
                             from jw_order_arch a, jw_comment b
                            where b.single_order_id = a.single_order_id
                            group by a.dish_id) comments
                  on comments.dish_id = a.dish_id


SELECT A.DISH_NAME, NVL(COMMENTS.SL, 0) PINGLUN
  FROM (SELECT DISH_NAME, DISH_ID FROM JW_DISH) A,
       (SELECT A.DISH_ID, COUNT(B.COMMENT_HEART) SL
          FROM JW_ORDER_ARCH A, JW_COMMENT B
         WHERE B.SINGLE_ORDER_ID = A.SINGLE_ORDER_ID
         GROUP BY A.DISH_ID) COMMENTS
 WHERE COMMENTS.DISH_ID = A.DISH_ID(+)

------解决方案--------------------
左连接 应该就这两种了left join on ,(+)