1

目前,我可以通过两个单独的 SELECT 语句获得所需的结果

SELECT 
COUNT(rl.refBiblioID) 
FROM biblioList bl 
LEFT JOIN refList rl ON bl.biblioID =  rl.biblioID 
GROUP BY bl.biblioID

SELECT
GROUP_CONCAT(
CONCAT_WS( ':', al.lastName, al.firstName )
ORDER BY al.authorID ) 
FROM biblioList bl 
LEFT JOIN biblio_author ba ON ba.biblioID = bl.biblioID
JOIN authorList al ON al.authorID = ba.authorID 
GROUP BY bl.biblioID

然而,像这样组合它们

SELECT
GROUP_CONCAT(
CONCAT_WS( ':', al.lastName, al.firstName )
ORDER BY al.authorID ),
COUNT(rl.refBiblioID) 
FROM biblioList bl 
LEFT JOIN biblio_author ba ON ba.biblioID = bl.biblioID
JOIN authorList al ON al.authorID = ba.authorID 
LEFT JOIN refList rl ON bl.biblioID =  rl.biblioID
GROUP BY bl.biblioID

导致作者结果列具有重复名称。如何在不使用 DISTINCT 的情况下从一个 SELECT 语句中获得所需的结果?有子查询?

4

1 回答 1

1

如果子选择是可以接受的,我相信您可以通过执行以下操作来实现这一点:

SELECT
  y.authors,
  COUNT(rl.refBiblioID) 
FROM
  (SELECT
    bl.biblioId,
    GROUP_CONCAT(
      CONCAT_WS(':', al.lastName, al.firstName)
      ORDER BY al.authorID) AS authors
  FROM biblioList bl
  LEFT JOIN biblio_author ba ON ba.biblioID = bl.biblioID
  JOIN authorList al ON al.authorID = ba.authorID 
  GROUP BY bl.biblioID) y
LEFT JOIN refList rl ON (y.biblioID =  rl.biblioID)
GROUP BY y.biblioID

另一种解决方案是添加DISTINCT到您的GROUP_CONCAT中,但这不是您想要的?

GROUP_CONCAT(
  DISTINCT(CONCAT_WS(':', al.lastName, al.firstName) ORDER BY al.authorID)),
于 2010-06-15T21:03:42.377 回答