我的代码选择了5个数据库并显示在表中,但是where语句不起作用
查询忽略的位置。
SELECT *
FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive where atender='$obj'
答案 0 :(得分:1)
我怀疑您希望WHERE
条件适用于联合中的每个子查询。如果需要,您必须向每个子查询添加一个WHERE
子句。但是,如果您确实想使用单个WHERE
子句,则可以包装并查询,然后对其进行子查询:
SELECT *
FROM
(
SELECT * FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive
) t
WHERE atender = '$obj';
旁注:请尽可能在PHP代码中使用准备好的语句。
答案 1 :(得分:1)
SELECT * FROM
( SELECT *
FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive)
AS derived
WHERE atender='$obj'