select social_members.*
, social_mcouple.*
WHERE m_id = '".$_SESSION['userid'] . "'
AND c_id = '".$_SESSION['userid'] . "'
Select all fields from so开发者_运维百科cial_members.* and if social_mcouple.c_id = $_SESSION['userid'] then Select all fields from social_mcouple.* as well. Can this be done with IF EXIST and if so how. Thanks
If I undertand you correctly, an outer join would be better here.
SELECT social_members.*
, social_mcouple.*
FROM social_members
LEFT OUTER JOIN social_mcouple
ON social_members.m_id=social_mcouple.c_id
WHERE m_id='".$_SESSION['userid']."'
This will retrieve rows for each member, and any corresponding rows from social_mcouple. If there are no corresponding rows in the social_mcouple table, then all the social_mcouple.* rows in the result will be NULL.
Depending upon your exact needs, and the relationship between the two tables, you may be better simply running this as two queries, one to retrieve matching rows from social_members, and the other to retrieve rows from social_mcouple. Whether you take the outer join or two separate queries will depend upon the "shape" of the data you want back.
And just in case, you were thinking it's not possible to return a different number of columns depending upon the data. (E.g. only the social_members columns if there are no applicable social_mcouple rows.)
精彩评论