[SOLVED] My MariaDB MYSQL query is returning results when it shouldn't

Issue

here is my query –

SELECT column_one FROM generation1 UNION SELECT column_one FROM generation2 WHERE column_one = value;

I am trying to query both tables for the value and i want PHP to execute a block of code if no result is found but the value in the where condition returns a result even when the value doesn’t exist on both tables.

Pls how can I make this query work and return the value of the "where" condition?

Solution

I think you want the WHERE logic to apply to the entire result set generated by the union query. If so, then you should subquery and apply the filter there:

SELECT column_one
FROM
(
    SELECT column_one FROM generation1
    UNION ALL
    SELECT column_one FROM generation2
) t
WHERE column_one = value;

Answered By – Tim Biegeleisen

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.