2014-05-25 65 views
0

我對以下MySQL查詢有一些問題,但找不到它的來源。MySQL錯誤:重複列

MySQL的告訴我,

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name '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

請勿在子查詢中選擇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

+0

我歸於不同的別名,並不意味着這樣一個事實有沒有重複的領域?就像一個會是annonce_dispo1_annonce_dispo_id,另一個會是annonce_dispo2.annonce_dispo_id,這意味着沒有重複應該發生(?)我認爲它也可以解決任何不明確的風險。 –

0

的問題是,你選擇的表annonce_dispo1annonce_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` 

我希望我幫助