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			[email protected]		jay1
208			[email protected]		jay2

 

SELECT count(em.email) email, count(us.user) users FROM members em LEFT JOIN members us ON em.email = '[email protected]' AND us.user = 'jay1'

 

i want it to return

 

EMAIL    USER

2            1

 

 

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/207165-left-join/
Share on other sites

its counting the amount of times that email is used

 

basically it would be joining these two queries

 


SELECT count(em.email) email FROM members em WHERE em.email = '[email protected]'

 

to this one

 

SELECT count(us.user) users FROM members us WHERE us.user = 'jay1'

Link to comment
https://forums.phpfreaks.com/topic/207165-left-join/#findComment-1084307
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 = '[email protected]' OR us.user = 'jay1'

 

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

Link to comment
https://forums.phpfreaks.com/topic/207165-left-join/#findComment-1084477
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
https://forums.phpfreaks.com/topic/207165-left-join/#findComment-1084488
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.