Select in two tables with clause WHERE?

Asked

Viewed 28 times

2

I need to check if a particular e-mail exists in two tables, if it exists in one of the two tables the user will not be able to register, facilitating would be the code below, the column email is present in the two tables;

SELECT * FROM tab1, tab2 WHERE email = :email

I’ve tried to

SELECT 
    usuarios.email, usr_pend.email
FROM usuarios, usr_pend 
    WHERE usuarios.email = :email
    OR usr_pend.email = :email

And I also tried

SELECT 
    usr.*, pen.* 
FROM usuarios usr 
    INNER JOIN usr_pend pen 
        ON usr.email = :email
        OR pen.email = :email

But both forms return 0, when in fact the email is present in one of them, as I should do this check?

  • 1

    I was able to understand the answers there, I already altered and put here, it worked perfectly, but thanks anyway ^

  • 1

    Anyway, I edited the answer simplifying to serve in simpler cases, if you want to take a look. This is as a general solution :)

  • Opa, thanks for simplifying, it became easier to understand what happens when we use UNION, thanks again ;)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.