mysql join只返回一个来自连接的结果

时间:2011-11-02 12:29:50

标签: mysql join relational-database

我有以下查询,

SELECT `candidates`.`candidate_id`, 
                    `candidates`.`first_name`, 
                    `candidates`.`surname`, 
                    `candidates`.`DOB`, 
                    `candidates`.`gender`, 
                    `candidates`.`talent`, 
                    `candidates`.`location`, 
                    `candidates`.`availability`,
                     DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) as `age`,
                    `candidate_assets`.`url`, 
                    `candidate_assets`.`asset_size`
            FROM `candidates`
            LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
            WHERE `candidates`.`availability` = 'yes'";

查询当前从连接表中返回多行,每个连接只能返回一个结果吗?

1 个答案:

答案 0 :(得分:0)

有可能。尝试使用GROUP_CONCAT功能,例如 -

SELECT
  c.`candidate_id`, 
  c.`first_name`, 
  c.`surname`, 
  c.`DOB`, 
  c.`gender`, 
  c.`talent`, 
  c.`location`, 
  c.`availability`,
  DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(c.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(c.`DOB`, '00-%m-%d')) as `age`,
  GROUP_CONCAT(ca.`url`) `url`,
  GROUP_CONCAT(ca.`asset_size`) `asset_size`
FROM `candidates` c
LEFT JOIN `candidate_assets` ca ON ca.`candidates_candidate_id` = c.`candidate_id`
WHERE c.`availability` = 'yes'
GROUP BY c.candidate_id