有没有办法在联合中引用子查询?
我正在尝试执行以下操作,并且希望避免使用临时表,但是子查询将从更大的数据集中提取,因此只执行一次是有意义的。
SELECT * FROM (SELECT * FROM ads WHERE state='FL' AND city='Maitland' AND page='home' ORDER BY RAND()) AS sq WHERE spot = 'full-banner' LIMIT 1
UNION
SELECT * FROM sq WHERE spot = 'leaderboard' LIMIT 1
UNION
SELECT * FROM sq WHERE spot = 'rectangle1' LIMIT 1
UNION
SELECT * FROM sq WHERE spot = 'rectangle2' LIMIT 1
.... etc,,
遗憾的是,不能为结果集的单个列指定 DISTINCT。