skip to Main Content

I have two tables where similar records stored. I have to search for existing record, where reference value matches one record if record is not found in first table, I need to search second table.

I tried querying like this but it returns all the records

SELECT * FROM keys WHERE EXISTS(SELECT * FROM store_1 WHERE store_1.key_number LIKE '%1726-1726280-17998%') or EXISTS(SELECT * FROM store_2 WHERE store_2.key_number LIKE '%1726-1726280-17998%');

2

Answers


  1. Chosen as BEST ANSWER

    As I understood with union I can return records with same number of columns

    SELECT * FROM store_1 WHERE store_1.key_number LIKE '%1726-1726280-17998%'
    UNION
    SELECT * FROM store_1 WHERE store_2.key_number LIKE '%1727-1727265-11276%';
    

  2. SELECT * FROM store_1
    WHERE 
        store_1.key_number LIKE '%1726-1726280-17998%' 
        OR store_2.key_number LIKE '%1727%'
    

    You can try this version if you’d not want a UNION of the tables, it combines both conditions into a single SELECT statement like you intended initially.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search