在表上显示时的PHP MySQL错误查询

时间:2018-10-11 03:27:07

标签: mysql mysqli

我的代码选择了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'

2 个答案:

答案 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'