0

table stockholders

stockholder_id    election_id    user_id    
1                 1              1                          
2                 1              2          
3                 2              3         

table users

user_id           user_type
1                 1
2                 1
3                 1
4                 1
5                 1


select 
    * 
from 
    tbl_users
left join tbl_stockholders
    on tbl_stockholders.user_id = tbl_users.user_id
where 
    user_type=1 
    and stockholders.user_id is null 
    and election_id <> 1

i want to search election_id not equal to 1 and user type equal to 1

stockholder_id    election_id    user_id      user_type
3                 2                 3            1         
null              null              4            1
null              null              5            1

this is an update

sorry my problem should be excluding the tbl_stockholders from the tbl_users with the parameter election_id.. because a problem exist when i have a duplicate user_id

table stockholders

stockholder_id    election_id    user_id    
1                 1              1                          
2                 1              2          
3                 2              3   
4                 1              3

in the previous answer this is the result when election_id<>2

stockholder_id    election_id    user_id      user_type
3                 1                 3            1         
null              null              4            1
null              null              5            1

this must be

stockholder_id    election_id    user_id      user_type      
null              null              4            1
null              null              5            1

this is my current not working code

select * from tbl_users where not exists (select * from tbl_stockholders where election_id <> 2)

3
  • Why do you have the predicate tbl_stockholders.user_id is null in your query? Especially since tbl_stockholders is on the left of your left join? Commented Oct 28, 2013 at 15:39
  • What would be your expected output? Commented Oct 28, 2013 at 15:39
  • tbl_users.user_id is null to exclude the user in the tbl_stockholders sorry is tbl_users not tbl_stockholders @eggyal Commented Oct 28, 2013 at 15:47

2 Answers 2

1

Try this:

SELECT * FROM users u
LEFT JOIN stockholders s ON u.user_id = s.user_id
WHERE u.user_type = 1 AND (s.election_id <> 1 OR s.election_id IS NULL)

Fiddle here.

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

3 Comments

SELECT * FROM users u LEFT JOIN stockholders s ON u.user_id = s.user_id WHERE u.user_type = 1 AND (s.election_id <> 1 OR s.election_id IS NULL)
thanks.. thats works.. just changing election_id=2 to election_id<>1
That is correct: The condition should be <> 1 rather than = 2. Due to the low amount of data the results matched :)
0

I think, this is what you should do: select * from tbl_stockholders left join tbl_users on tbl_users.user_id = tbl_stockholders.user_id where (user_type=1 and election_id <> 1) I don't think you intend to select null user_ids. Also that would invalidate the join i.e you are saying joined the two tables based user_id but select null user_id from the first table.

1 Comment

hmm.. tbl_stockholders is the selection of users on tbl_users , then i want to search a users does not exist in tbl_stockholders. and not equal election_id ,, sorry for my english i cant explain it clearly.. sorry

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.