tags:

views:

69

answers:

1

Is there a way to avoid adding a second LEFT JOIN for the table "social_mcouple" to query where social_members.m_id = social_mcouple.c_id below?

$res = @mysql_query("SELECT *, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted FROM social_members 
        LEFT JOIN social_member_types ON t_id=m_type WHERE m_user='".$en['user']."'");
+1  A: 

If there will always be a social_mcouple that corresponds to social_members, or you're only interested in rows where there is a correspondence then you may use an INNER JOIN. If you need all social_members regardless of whether there is a corresponding social_mcouple then you will need a LEFT JOIN. The LEFT JOIN will give you all rows with social_mcouple.* set to NULL where there is not a match.

The performance hit will really depend on the size of your datasets.

EDIT: adding a sample UNION query.

$res = @mysql_query("
(SELECT social_members.*, social_member_types.*, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted,
  NULL AS mcouple1, NULL AS mcouple2, NULL AS mcouple3
FROM social_members 
LEFT JOIN social_member_types ON t_id=m_type
WHERE m_user='".$en['user']."' AND m_type != 2)

UNION

(SELECT social_members.*, social_member_types.*, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted,
    social_mcouple.mcouple1, social_mcouple.mcouple2, social_mcouple.mcouple3
FROM social_members 
LEFT JOIN social_member_types ON t_id=m_type
     JOIN social_mcouple ON social_members.m_id = social_mcouple.c_id
WHERE m_user='".$en['user']."' AND m_type = 2)
");
thetaiko
I'm only going to need all the fields from social_mcouple IF social_member.m_id = social_mcouple.c_id IF not then the social_mcouple table will not be needed at all. So if I read your answer correctly in my case LEFT JOIN is what i'll needed and not INNER JOIN?
acctman
Also is there a way to write the statement almost like an IF statement WHERE IF m_type = 2 Then run the LEFT JOIN for social_mcouple IF not keep the statement the same?
acctman
@acctman - that is correct.
thetaiko
@acctman - you have a couple of options there. You could write a `UNION` with the first `SELECT` setting `NULL AS c_id, NULL AS ...` for each `social_mcouple` field with the added `WHERE m_type != 2` and the second `SELECT` using `WHERE m_type = 2`
thetaiko
@acctman, that would also give you the benefit of using an `INNER JOIN` rather than a `LEFT JOIN` because there will always be a match if `m_type = 2`, right?
thetaiko
@thetaiko yes there will always be a match in social_mcouple for .c_id = m_id if m_type = 2
acctman
hmm I've never used a Union or Inner before, all the examples I've googled seen dont apply to what I'm trying to do. can you assist me with a working example
acctman
@acctman - BTW - how about a little upvote love for my time?
thetaiko
@thetaiko I was trying to upvote earlier... but its telling me reputation needs to be at 15 in order to upvote.
acctman
@thetaiko the upgraded my rep, so I was able to upvote. question about the sample, if I did NULL AS * then social_mcouple.* in the second select would that be valid? Because all the fields in mcouple need to be selected (6 fields). Thanks for all the help
acctman
NULL AS * won't work. You'll have to specify the names of all the fields. So if mcouple has fields 'a', 'b', 'c' ... then you'll need NULL AS a, NULL AS b, NULL AS c, etc.
thetaiko