mysql查询对某些结果进行排序,但是可能存在或不存在连接数据

时间:2010-03-03 04:32:26

标签: sql mysql

SELECT PLD_LINK.ID, PLD_LINK.TITLE, PLD_LINK.URL, PLD_CATEGORY.TITLE, TLL_SORT_STATUS.status, PLD_LINK_COMMENT.DATE_ADDED
    FROM PLD_LINK, PLD_CATEGORY, TLL_SORT_STATUS, PLD_LINK_COMMENT 
    WHERE PLD_LINK.CATEGORY_ID = PLD_CATEGORY.ID 
        AND PLD_LINK.ID = TLL_SORT_STATUS.link_id 
        AND PLD_LINK.PAGERANK BETWEEN -1 AND 2
        AND PLD_LINK.CATEGORY_ID IN (1291,2113,2664)
        ORDER BY COUNT(PLD_LINK.ID = PLD_LINK_COMMENT.LINK_ID) ASC

这是我的查询。我想在ORDER BY之前得到所有匹配的结果。现在,PLD_LINK.ID中的某些ID可能位于表PLD_LINK_COMMENT中,但有些可能不是。因此,当mysql到达ORDER BY时,我想根据以下标准按升序排序结果:不在PLD_LINK_COMMENT中的ID应该是第一个,之后是基于每个ID的注释数量的ID 。 (一个ID可以没有,1,2 ......评论。)

1 个答案:

答案 0 :(得分:1)

您需要在三个主要表格和PLD_LINK_COMMENT之间进行外部联接。

外部联接意味着您获得PLD_LINK_COMMENT.LINK_ID的空值,这意味着COUNT将为PLD_LINK.ID返回0,没有匹配的链接注释,以及其他注释的相应数量的注释。

您需要按非聚合列进行分组......但这表明M.Date_Added可能不是您想要的GROUP BY;并且评论中的问题/答案显示我们需要最近的评论日期,或者一些荒谬的早期数据,例如'0001-01-01'(SQL中最早的有效日期)或'0000-00-00'(伪 - MySQL使用的日期。

因此:

SELECT K.ID, K.Title, K.URL, C.Title, S.Status,
       MAX(NVL(M.Date_Added, DATE '0001-01-01')) AS MostRecentComment
  FROM Pld_Link AS K JOIN Pld_Category AS C ON K.Category_ID = C.ID
       JOIN Tll_Sort_Status AS C ON K.ID = S.Link_ID
       OUTER JOIN Pld_Link_Comment AS M ON K.ID = M.Link_ID
 WHERE K.PageRank BETWEEN -1 AND 2
   AND K.Category_ID IN (1291,2113,2664)
 GROUP BY K.ID, K.Title, K.URL, C.Title, S.Status
 ORDER BY COUNT(M.Link_ID) ASC

您还需要您的DBMS(您说的MySQL),以允许您按SELECT列表中未列出的列进行排序。


经过测试的SQL

在MacOS X 10.6.2上使用IBM Informix Dynamic Server 11.50进行测试。我使用了DATE('0001-01-01'),因为IDS有一个名为DATE的函数需要括号,我使用'0001-01-01'因为IDS不会将'0000-00-00'识别为有效日期。

SELECT K.ID, K.Title, K.URL, C.Title, S.STATUS,
       MAX(NVL(M.Date_Added, DATE('0001-01-01'))) AS MostRecentComment,
  FROM PLD_Link AS K JOIN PLD_Category AS C ON K.Category_ID = C.ID
       JOIN Tll_Sort_Status AS S ON K.ID = S.Link_ID
       LEFT OUTER JOIN PLD_Link_Comment AS M ON K.ID = M.Link_ID
 WHERE K.PageRank BETWEEN -1 AND 2
   AND K.Category_ID IN (1291,2113,2664)
 GROUP BY K.ID, K.Title, K.URL, C.Title, S.STATUS
 ORDER BY COUNT(M.Link_ID)

结果:

id  title                 url                       title   status  mostrecent
4   #4: This should show  example.com/?id=4;c=2113  Cat 2113  0  0001-01-01
3   #3: This should show  example.com/?id=3;c=1291  Cat 1291 -1  2010-03-10
7   #7: This should show  example.com/?id=7;c=2664  Cat 2664  3  2010-07-11
5   #5: This should show  example.com/?id=5;c=2113  Cat 2113  1  2010-05-17
2   #2: This should show  example.com/?id=2;c=1291  Cat 1291 -2  2010-02-13

备选,选择评论计数,以显示数据以正确的顺序显示。

SELECT K.ID, K.Title, K.URL, C.Title, S.STATUS,
       MAX(NVL(M.Date_Added, DATE('0001-01-01'))) AS MostRecentComment,
       COUNT(M.Link_ID) AS CommentCount
  FROM PLD_Link AS K JOIN PLD_Category AS C ON K.Category_ID = C.ID
       JOIN Tll_Sort_Status AS S ON K.ID = S.Link_ID
       LEFT OUTER JOIN PLD_Link_Comment AS M ON K.ID = M.Link_ID
 WHERE K.PageRank BETWEEN -1 AND 2
   AND K.Category_ID IN (1291,2113,2664)
 GROUP BY K.ID, K.Title, K.URL, C.Title, S.STATUS
 ORDER BY CommentCount ASC;

结果集:

id title                 url                       title  status  recent  count
4  #4: This should show  example.com/?id=4;c=2113  Cat 2113  0  0001-01-01  0
3  #3: This should show  example.com/?id=3;c=1291  Cat 1291 -1  2010-03-10  1
7  #7: This should show  example.com/?id=7;c=2664  Cat 2664  3  2010-07-11  1
5  #5: This should show  example.com/?id=5;c=2113  Cat 2113  1  2010-05-17  2
2  #2: This should show  example.com/?id=2;c=1291  Cat 1291 -2  2010-02-13  3

示例数据库

PLD_Category

CREATE TABLE PLD_Category
(
    ID      SERIAL NOT NULL PRIMARY KEY,
    Title   VARCHAR(32) NOT NULL
);

INSERT INTO PLD_Category VALUES(3961, 'Cat 3961');
INSERT INTO PLD_Category VALUES(1291, 'Cat 1291');
INSERT INTO PLD_Category VALUES(2113, 'Cat 2113');
INSERT INTO PLD_Category VALUES(2664, 'Cat 2664');

PLD_Link

CREATE TABLE PLD_Link
(
    ID          SERIAL NOT NULL PRIMARY KEY,
    Category_ID INTEGER NOT NULL REFERENCES PLD_Category,
    Title       VARCHAR(32) NOT NULL,
    PageRank    INTEGER NOT NULL,
    URL         VARCHAR(64) NOT NULL
);

INSERT INTO PLD_Link
    VALUES(1, 3961, '#1: This should not',  0, 'example.com/?id=1;c=3961');
INSERT INTO PLD_Link
    VALUES(2, 1291, '#2: This should show', 0, 'example.com/?id=2;c=1291');
INSERT INTO PLD_Link
    VALUES(3, 1291, '#3: This should show', 0, 'example.com/?id=3;c=1291');
INSERT INTO PLD_Link
    VALUES(4, 2113, '#4: This should show', 0, 'example.com/?id=4;c=2113');
INSERT INTO PLD_Link
    VALUES(5, 2113, '#5: This should show', 0, 'example.com/?id=5;c=2113');
INSERT INTO PLD_Link
    VALUES(6, 3961, '#6: This should not',  0, 'example.com/?id=6;c=3961');
INSERT INTO PLD_Link
    VALUES(7, 2664, '#7: This should show', 0, 'example.com/?id=7;c=2664');
INSERT INTO PLD_Link
    VALUES(8, 2664, '#8: This should show', 0, 'example.com/?id=8;c=2664');

TLL_Sort_Status

CREATE TABLE TLL_Sort_Status
(
    STATUS      INTEGER NOT NULL,
    Link_ID     INTEGER NOT NULL REFERENCES PLD_Link
);

INSERT INTO TLL_Sort_Status VALUES(-3, 1);
INSERT INTO TLL_Sort_Status VALUES(-2, 2);
INSERT INTO TLL_Sort_Status VALUES(-1, 3);
INSERT INTO TLL_Sort_Status VALUES( 0, 4);
INSERT INTO TLL_Sort_Status VALUES(+1, 5);
INSERT INTO TLL_Sort_Status VALUES(+2, 6);
INSERT INTO TLL_Sort_Status VALUES(+3, 7);
INSERT INTO TLL_Sort_Status VALUES(+4, 1);

PLD_Link_Comment

CREATE TABLE PLD_Link_Comment
(
    Link_ID     INTEGER NOT NULL REFERENCES PLD_Link,
    Date_Added  DATE NOT NULL,
    COMMENT     VARCHAR(64) NOT NULL
);

INSERT INTO PLD_Link_Comment VALUES(1, DATE('2010-01-11'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(2, DATE('2010-02-11'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(2, DATE('2010-02-12'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(2, DATE('2010-02-13'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(3, DATE('2010-03-10'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(5, DATE('2010-05-15'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(5, DATE('2010-05-17'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(6, DATE('2010-06-10'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(6, DATE('2010-06-12'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(6, DATE('2010-06-14'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(6, DATE('2010-06-19'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(7, DATE('2010-07-11'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(8, DATE('2010-08-12'), 'Vacuous comment');
INSERT INTO PLD_Link_Comment VALUES(8, DATE('2010-08-13'), 'Vacuous comment');