MySQL错误:重复列

时间:2014-05-25 20:02:06

标签: mysql sql

我对以下MySQL查询有点问题而我无法找到它的来源。

MySQL告诉我

  

SQLSTATE [42S21]:列已存在:1060重复的列名   ' annonce_dispo_id'

SELECT MAX(max_price) AS `max_price`,
       COUNT(*) AS `nb_annonces`,
       SUM(nb_dispo) AS `nb_dispo`
FROM
  (SELECT `annonce`.`id`,
          CEIL(MAX(price)*1.16) AS `max_price`,
          COUNT(DISTINCT annonce.id) AS `nb_annonces`,
          COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
          `annonce_dispo1`.*,
          `annonce_dispo2`.*
   FROM `annonce`
   LEFT JOIN `annonce_dispo` AS `annonce_dispoo` ON (annonce_dispoo.annonceId = annonce.id
                                                     AND STR_TO_DATE(annonce_dispoo.dispo_date, '%d/%m/%Y') >= CURDATE())
   INNER JOIN `annonce_dispo` AS `annonce_dispo1` ON annonce.id = annonce_dispo1.annonceId
   INNER JOIN `annonce_dispo` AS `annonce_dispo2` ON annonce.id = annonce_dispo2.annonceId
   WHERE ((annonce.city IN
             (SELECT `cities`.`id`
              FROM `cities`
              WHERE (cities.label LIKE 'lyon%'))
           OR annonce.zipcode = 'lyon')
          OR (annonce.city LIKE '28674'
              OR annonce.zipcode = '28674'))
     AND (annonce_dispo1.dispo_date = '27/05/2014')
     AND (annonce_dispo1.disponibility = 'available')
     AND (annonce_dispo2.dispo_date = '31/05/2014')
     AND (annonce_dispo2.disponibility = 'available')
     AND (annonce.visible = 1)
     AND (annonce.completed = 1)
   GROUP BY `annonce`.`id` HAVING (nb_dispo >= 1)) AS `t`

我想在我使用它们的每个JOIN中为表格提供了一个不同的别名,并且无法真正指出还有什么可能输出这样的错误。

2 个答案:

答案 0 :(得分:2)

不要在子查询中选择annonce_dispo1.*annonce_dispo2.*,而是返回重复的列名。而是选择所需的字段并相应地使用别名。

SELECT MAX(max_price) AS `max_price`,
       COUNT(*) AS `nb_annonces`,
       SUM(nb_dispo) AS `nb_dispo`
FROM
  (SELECT `annonce`.`id`,
          CEIL(MAX(price)*1.16) AS `max_price`,
          COUNT(DISTINCT annonce.id) AS `nb_annonces`,
          COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
          `annonce_dispo1`.field, `annonce_dispo1`.otherfield,
          `annonce_dispo1`.field as field2, `annonce_dispo1`.otherfield as otherfield2
   FROM `annonce`
   LEFT JOIN `annonce_dispo` AS `annonce_dispoo` ON (annonce_dispoo.annonceId = annonce.id
                                                     AND STR_TO_DATE(annonce_dispoo.dispo_date, '%d/%m/%Y') >= CURDATE())
   INNER JOIN `annonce_dispo` AS `annonce_dispo1` ON annonce.id = annonce_dispo1.annonceId
   INNER JOIN `annonce_dispo` AS `annonce_dispo2` ON annonce.id = annonce_dispo2.annonceId
   WHERE ((annonce.city IN
             (SELECT `cities`.`id`
              FROM `cities`
              WHERE (cities.label LIKE 'lyon%'))
           OR annonce.zipcode = 'lyon')
          OR (annonce.city LIKE '28674'
              OR annonce.zipcode = '28674'))
     AND (annonce_dispo1.dispo_date = '27/05/2014')
     AND (annonce_dispo1.disponibility = 'available')
     AND (annonce_dispo2.dispo_date = '31/05/2014')
     AND (annonce_dispo2.disponibility = 'available')
     AND (annonce.visible = 1)
     AND (annonce.completed = 1)
   GROUP BY `annonce`.`id` HAVING (nb_dispo >= 1)) AS `t`

请参阅此处查看不起作用的示例:

http://sqlfiddle.com/#!2/9bb13/1

答案 1 :(得分:0)

问题是您要选择表 annonce_dispo1 annonce_dispo2 中的所有列。 您归因于不同表名的事实并不意味着没有重复的列名。 我的意思是,你应该使用 [表名]。[列名]
例如:

  (SELECT `annonce`.`id`,
      CEIL(MAX(price)*1.16) AS `max_price`,
      COUNT(DISTINCT annonce.id) AS `nb_annonces`,
      COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
      `annonce_dispo1`.annonce_dispo_id AS `column1`,
      `annonce_dispo2`.annonce_dispo_id AS `column2`

我希望我能帮助