Jump to content

Recommended Posts

Hi.
I wondered how to do an SQL left join with a normal right join. what's the protocol for that?

I need to do a standard query (which works) and then have a OR command containing the SQL equivalent of "people in the members table, who don't have anything uploaded to the pictures table" but I dunno how to do it.

Any suggestions?

I have looked at previous Left join examples (as I think that's what's needed) but none of them work.
Link to comment
https://forums.phpfreaks.com/topic/35988-left-join/
Share on other sites

I'm trying to select all members that have an activated picture and also all members that have not uploaded a picture, but not members who have uploaded a picture and not had it activated.

I'm using this code:
[code]$sql= "SELECT pictures.user_id,
pictures.id,
pictures.url
FROM pictures, member
WHERE (member.id=pictures.user_id
&& pictures.active='y'
&& pictures.primary='y'
".$preferences.") [/code]
That bit's working fine...
Then I need to add something that says "and members who've not uploaded a picture".

Ideally in the same query.

Thanks for the suggestion, but I don't know how to do both.
Link to comment
https://forums.phpfreaks.com/topic/35988-left-join/#findComment-171089
Share on other sites

[code]
SELECT
members.id, pictures.id, pictures.url
FROM
member
LEFT JOIN
pictures
ON
member.id = pictures.user_id
WHERE
(pictures.active='y' && pictures.primary='y')
OR
(pictures.user_id IS NULL)
$preferences
[/code]

I'm assuming that $preferences doesn't have anything to do with the WHERE clause.
Link to comment
https://forums.phpfreaks.com/topic/35988-left-join/#findComment-171144
Share on other sites

actually $preferences was part of the where clause...
My SQL statement now looks like this (after processing the preferences variable):

[code]SELECT
member.id, pictures.id, pictures.url
FROM
member
LEFT JOIN
pictures ON member.id = pictures.user_id
WHERE
(pictures.active='y'
&& pictures.primary='y'
&&(member.sex='f')
&&(member.orientation='straight')
&& (member.age BETWEEN 26 AND 32)
&& (member.area='England-Southwest'))
OR (pictures.url IS NULL)[/code]

There seems to be an error, but I'm not too great at SQL. URGH...
thanks for your help so far though
Link to comment
https://forums.phpfreaks.com/topic/35988-left-join/#findComment-171157
Share on other sites

[quote author=gerkintrigg link=topic=124335.msg515413#msg515413 date=1169997488]
actually $preferences was part of the where clause...
My SQL statement now looks like this (after processing the preferences variable):

[code]SELECT
member.id, pictures.id, pictures.url
FROM
member
LEFT JOIN
pictures ON member.id = pictures.user_id
WHERE
(pictures.active='y'
&& pictures.primary='y'
&&(member.sex='f')
&&(member.orientation='straight')
&& (member.age BETWEEN 26 AND 32)
&& (member.area='England-Southwest'))
OR (pictures.url IS NULL)[/code]

There seems to be an error, but I'm not too great at SQL. URGH...
thanks for your help so far though
[/quote]

Looks like a "(" is missing from the start of the clause but after seeing what $preferences contains (ie limits based on the members table) the following should be what you're looking for.

[code]
SELECT
members.id, pictures.id, pictures.url
FROM
member
LEFT JOIN
pictures
ON
member.id = pictures.user_id
WHERE
($preferences)
&&
(
    (pictures.active='y' && pictures.primary='y')
    OR
    (pictures.user_id IS NULL)
)
[/code]

Post the error messages  if you get any.
Link to comment
https://forums.phpfreaks.com/topic/35988-left-join/#findComment-171163
Share on other sites

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') && ( (pictures.active='y' && pictures.primary='y') OR

the problem is that the preferences variable can be empty, depending on whether users set their preferences or not.
Link to comment
https://forums.phpfreaks.com/topic/35988-left-join/#findComment-171826
Share on other sites

A quick fix would be to test if the variable contains anything and only add it to the WHERE clause if it does.
[code]
$query = 'SELECT
members.id, pictures.id, pictures.url
FROM
member
LEFT JOIN
pictures
ON
member.id = pictures.user_id
WHERE ';
if (isset($preferences) && $preferences != '')
{
    $query .= "($preferences) && ";

}
$query .= '(
    (pictures.active='y' && pictures.primary='y')
    OR
    (pictures.user_id IS NULL)
) ';
[/code]
Link to comment
https://forums.phpfreaks.com/topic/35988-left-join/#findComment-171845
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.