views:

71

answers:

1

At the moment I can get the results I need with two seperate SELECT statements

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

Combining them like this however

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

causes the author result column to have duplicate names. How can I get the desired results from one SELECT statement without using DISTINCT? With subqueries?

A: 

If subselects are acceptable, I believe you could make this happen by doing something like this:

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

Another solution would be to add DISTINCT to your GROUP_CONCAT, but that was not what you wanted?

GROUP_CONCAT(
  DISTINCT(CONCAT_WS(':', al.lastName, al.firstName) ORDER BY al.authorID)),
Ivar Bonsaksen