where子句中的City_id含糊不清

时间:2013-04-14 05:14:38

标签: mysql codeigniter codeigniter-2 codeigniter-url

我已经编写了查询,但它在where子句中显示了城市ID

SELECT
  `a`.`name`,
  `a`.`company`,
  `a`.`city`,
  `a`.`country`,
  `a`.`phone`,
  `a`.`type_of_enquiry`,
  `b`.`enquiry_id`,
  `a`.`hearaboutus`,
  `a`.`email`,
  `a`.`comments`,
  `a`.`address`,
  `c`.`id`,
  `c`.`city_id`
FROM (`sobha_enquiry` a)
  LEFT JOIN `sobha_enquiryzone` b
    ON `b`.`enquiry_id` = `a`.`id`
  LEFT JOIN `sobha_admin` c
    ON `c`.`city_id` = `b`.`city_id`
WHERE `city_id` = '2'
GROUP BY `a`.`id`
ORDER BY `id` desc, `a`.`id` DESC

请帮助我!!!!!!!!!!

2 个答案:

答案 0 :(得分:3)

SELECT
  `a`.`name`,
  `a`.`company`,
  `a`.`city`,
  `a`.`country`,
  `a`.`phone`,
  `a`.`type_of_enquiry`,
  `b`.`enquiry_id`,
  `a`.`hearaboutus`,
  `a`.`email`,
  `a`.`comments`,
  `a`.`address`,
  `c`.`id`,
  `c`.`city_id`
FROM (`sobha_enquiry` a)
  LEFT JOIN `sobha_enquiryzone` b
    ON `b`.`enquiry_id` = `a`.`id`
  LEFT JOIN `sobha_admin` c
    ON `c`.`city_id` = `b`.`city_id`
WHERE `a`.`city_id` = '2'
GROUP BY `a`.`id`
ORDER BY `c`.`id` desc, `a`.`id` DESC

在你的where子句中,你应该提供b或c,因为使用了别名,并且两个表中的city_id列都存在令人困惑的mysql

答案 1 :(得分:2)

问题出在WHERE子句中,您需要指定哪些表将搜索city_id sice,它们都包含相同的列名。它可以来自sobha_enquiryzone,也可以来自sobha_admin

SELECT  a.NAME,
        a.company,
        a.city,
        a.country,
        a.phone,
        a.type_of_enquiry,
        b.enquiry_id,
        a.hearaboutus,
        a.email,
        a.comments,
        a.address,
        c.id,
        c.city_id
FROM    sobha_enquiry a
        LEFT JOIN sobha_enquiryzone b
                ON b.enquiry_id = a.id
        LEFT JOIN sobha_admin c
                ON c.city_id = b.city_id
WHERE   b.city_id = '2' -- or c.city_id = '2' (both will yield the same result)
GROUP   BY a.id
ORDER   BY id DESC, a.id DESC