0

there's my code that select 5 databases and display in a table, but the where statement not work

The where is ignored by the query.

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'
4
  • 2
    "not work" is a completely unhelpful description. What's it supposed to do? What's it actually do? Commented Oct 11, 2018 at 3:27
  • The query is ignoring the where in the statement. Commented Oct 11, 2018 at 3:29
  • On where not work, and with only where is the same problem Commented Oct 11, 2018 at 3:33
  • Do you have a column named as "atender" and any row in the column has "$obj" as data? Commented Oct 11, 2018 at 4:02

2 Answers 2

1

I suspect that you want the WHERE criteria to apply to every subquery in the union. If you want that, you'll have to add a WHERE clause to each subquery. But, if you really do want to use a single WHERE clause, you can wrap your union query and then subquery it:

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';

Side note: Please use prepared statements in your PHP code wherever possible.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep and now i use prepared statements but im new with the unions, thank you for the help tim.
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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.