我正在使用 MySQL 5.5.16
我有以下查询,它本身就可以正常工作。
SELECT DISTINCT i.id,
CASE WHEN e.date > '2012-10-16'
THEN e.date
ELSE '9999-99-99'
END AS date,
CASE WHEN e.date > '2012-10-16'
THEN time
ELSE '99-99-99'
END AS time
FROM items AS i
LEFT JOIN expiration AS e ON ( e.item_id = i.id )
WHERE (
(
data >= '2012-10-16'
AND e.valid=1
)
OR i.never_expires=1
)
AND i.valid=1
ORDER BY date ASC , time ASC
LIMIT 0 , 10
但是,当我将它包含在更大的查询中时,我得到一个错误Column 'date' in where clause is ambiguous
。这是一个示例,上面的查询位于 a 内JOIN
:
SELECT i.id, i.title, i.never_expires,
CASE WHEN e.date> '2012-10-16'
THEN e.date
ELSE '9999-99-99'
END AS date,
CASE WHEN e.date > '2012-10-16'
THEN e.time
ELSE '99-99-99'
END AS time, i.item_price AS price
FROM items AS i
LEFT JOIN expiration AS e ON ( e.item_id = i.id )
JOIN (
SELECT DISTINCT i.id,
CASE WHEN e.date > '2012-10-16'
THEN e.date
ELSE '9999-99-99'
END AS date,
CASE WHEN e.date > '2012-10-16'
THEN time
ELSE '99-99-99'
END AS time
FROM items AS i
LEFT JOIN expiration AS e ON ( e.item_id = i.id )
WHERE (
(
data>= '2012-10-16'
AND e.valid=1
)
OR i.never_expires=1
)
AND i.valid=1
ORDER BY date ASC , time ASC
LIMIT 0 , 10
) AS ilist ON (i.id=ilist.id)
WHERE (
(
date >= '2012-10-16'
AND e.valid=1
)
OR i.never_expires=1
)
AND i.valid=1
ORDER BY dateASC , time ASC
为什么它声称这date
是模棱两可的?
PS我试过用a替换内部查询date
的一部分,但这只会引发另一个错误......AS date
inner_date
Unknown column 'inner_date' in 'where clause'