合并查询结果,优化?

时间:2016-11-04 06:31:30

标签: mysql sql

mysql,两个表:test(一个)和review(很多)。 我的目标:从相应数字中查看一个

SELECT t.ID,t.TITLE,COALESCE(COUNT(r.ID),0) `count`
FROM `test` t 
LEFT OUTER JOIN review r
    ON t.ID = r.REVIEW_OBJ_ID 
WHERE r.REVIEW_TYPE = '4'
ORDER BY `count` DESC;

输出:

ID                                  TITLE      count
402884f657e0a6d20157e0a82cc90000    brother    2

测试表(一小部分数据)

SELECT t.ID,t.TITLE
FROM `test` t;

输出:

ID                                  TITLE
40284c8157ad8e7d0157ad8f86880000    1234567890123456789012345
402884f657e0a6d20157e0a82cc90000    brother
402884f657e0a6d20157e11967a20036    fg
402884f657e51eff0157e54cd8610004    AAA
402884f657e652fb0157e65642750000    BBB
0000000057f4dc900157f4ea9edd0000    VVV
00000000580065c5015800746d750000    CCC
00000000580065c501581d9f04f0000b    TTT

我希望得到这个:

ID                                  TITLE                      count 
402884f657e0a6d20157e0a82cc90000    brother                    2
402884f657e652fb0157e65642750000    BBB                        0
00000000580065c501581d9f04f0000b    TTT                        0
402884f657e0a6d20157e11967a20036    fg                         0
0000000057f4dc900157f4ea9edd0000    VVV                        0
40284c8157ad8e7d0157ad8f86880000    1234567890123456789012345  0
402884f657e51eff0157e54cd8610004    AAA                        0
00000000580065c5015800746d750000    CCC                        0

所以,我试过这个并且它有效:

SELECT t.ID,t.TITLE, COALESCE(r.c,0) `count`
FROM `test` t 
LEFT OUTER JOIN
(
    SELECT r.REVIEW_OBJ_ID obj_id, COUNT(r.ID) c 
    FROM review r,`test` t
    WHERE r.REVIEW_TYPE = '4' 
        AND t.ID = r.REVIEW_OBJ_ID
) r ON r.obj_id = t.ID
ORDER BY `count` DESC;

但我有两个问题:

  1. 感觉我可以使用一次性选择来找出结果,但我使用了两次选择。我可以优化它吗?
  2. 在测试表字段中添加count(冗余),是否是更好的选择。
  3. / REVIEW_TYPE和REVIEW_OBJ_ID决定审核哪个对象,就像我使用" REVIEW_TYPE =' 4'"联系test表/

    drop table if exists user_doctor_review;
    create table review
    (
       ID                   varchar(64) not null,
       USER_ID              varchar(64),
       DOCTOR_ID            varchar(64),
       REVIEW_TYPE          varchar(1),
       REVIEW_OBJ_ID        varchar(64),
       SERVICE_SCORE        int(6),
       REVIEW_CONTENT       varchar(600),
       REVIEW_TIME          datetime,
       POID                 varchar(64),
       IS_ANONYMITY         varchar(1),
       CHECKED_STATUS       varchar(1),
       STATUS               varchar(1),
       REPLY_CONTENT        varchar(600),
       REPLY_TIME           datetime,
       DOCTOR_IS_READ       varchar(1),
       primary key (ID)
    );
    

1 个答案:

答案 0 :(得分:0)

是的,您可以使用此查询的一个SELECT语句

来执行此操作
SELECT test.ID, test.TITLE, count(review.ID) as count from test
left join review on test.ID = review.REVIEW_OBJ_ID
where review.REVIEW_TYPE = 4 or review.ID is null
group by test.ID

我在这里创建了一个SQL小提琴:http://sqlfiddle.com/#!9/c6a178/15

<强>说明: 关键点是:

or review.ID is null

因为它会使查询列表中没有评论的测试,

group by test.ID

这将获得与测试相关的正确评论计数。

结果:

ID                                  TITLE                       count
--------------------------------    -------------------------   -----
0000000057f4dc900157f4ea9edd0000    VVV                         0
00000000580065c5015800746d750000    CCC                         0
00000000580065c501581d9f04f0000b    TTT                         1
40284c8157ad8e7d0157ad8f86880000    1234567890123456789012345   0
402884f657e0a6d20157e0a82cc90000    brother                     2
402884f657e0a6d20157e11967a20036    fg                          0
402884f657e51eff0157e54cd8610004    AAA                         0
402884f657e652fb0157e65642750000    BBB                         0