Home > Back-end >  query with two tables with condition OR
query with two tables with condition OR

Time:12-06

So I have two tables and I want do query to select which login and pwd same in the two table to get the result so I do that query in below but return result the one table and the other when I changer the login contains in the other table any result so I do OR in MYsql :

select tab1.*,tab2.* from tab1,tab2 where 
tab1.login='[email protected]' and tab1.pwd='123' 
or tab2.email='[email protected]' and tab2.pwd='123';

Thanks in advance

CodePudding user response:

you can use brackets to ensure correct order of operations

select tab1.*,tab2.* from tab1,tab2 
where ( tab1.login='[email protected]' and tab1.pwd='123' ) or (tab2.email='[email protected]' and tab2.pwd='123') ;

CodePudding user response:

you can try to change the query like this

SELECT tab1.*, tab2.*
FROM tab1
LEFT JOIN tab2
ON tab1.login = tab2.email
AND tab1.pwd = tab2.pwd
WHERE tab1.login = '[email protected]'
AND tab1.pwd = '123';
  • Related