Jump to content

left join


M.O.S. Studios

Recommended Posts

Im sure this is easy, but this is what i have

 

index			email			user 
207			123@hotmail.com		jay1
208			123@hotmail.com		jay2

 

SELECT count(em.email) email, count(us.user) users FROM members em LEFT JOIN members us ON em.email = '123@hotmail.com' AND us.user = 'jay1'

 

i want it to return

 

EMAIL    USER

2            1

 

 

 

thanks in advance

Link to comment
Share on other sites

Oh, I see know -- that's a strange query.

 

Easiest way is to switch your WHERE to an OR, and then use IFs to check the boolean condition.

 

SELECT 
SUM( IF( em.email = '123.hotmail.com', 1, 0 ) ) AS email, 
SUM( IF( us.user = 'jay', 1, 0 ) ) AS users 
FROM members em LEFT JOIN members us ON em.email = '123@hotmail.com' OR us.user = 'jay1'

 

But that's not ideal for index usage -- why not just UNION the results?

Link to comment
Share on other sites

Never heard of union before,

 

after doing some research i think this is perfect.

 

this is what i came up with

 

(SELECT count(em.email) email FROM members em WHERE email = '123.hotmail.com') UNION (SELECT count(us.user) user FROM members us WHERE user = 'jay1')

 

only problem is that its returning the two values under one column, any idea why

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.