Jump to content

Subquery Help


adam84

Recommended Posts

SELECT pictureLink FROM userPictures, (SELECT count(pictureID) FROM userPictures WHERE userID = 11 AND relType = 1 AND pictureGallery = 1 LIMIT 1) as galCount, 
(SELECT count(pictureID) FROM userPictures WHERE userID = 11 AND relType = 1 AND picturePicture = 1 LIMIT 1) as priCount 
WHERE userID = 11 AND relType = 1 LIMIT 1

 

When I run this query, all I get returned is the pictureLink.

If i ran the last two sub queries, i get a value of 1 returned.

 

How come, when it is all put together, only the pictureLink row gets returned?

Link to comment
Share on other sites

1 - get the pictureLink. I am able to do that

2 - I want to see if there is at least 1 gallery picture for a certain user and certain relationship

3 - I want to see if there is at least 1 private picture for a certain user and certain relationship

 

I can do it in different queries, but I just thought one query would be easier

Link to comment
Share on other sites

This is the table structure

 

Table userPictures:

 

pictureID - int auto

userID - int

relType - int  - a picture can be used in different profiles (we can just use 1)

pictureLink - varchar  - url of pic

pictureMain - int    - is this picture the main picture

pictureGallery - int - is this picture a gallery picture

picturePrivate - int - is this picture a private picture

pictureCreated - datetime

 

Ok, what I want returned is three different things.

1 - the 'pictureLink' of a 'pictureMain'

2 - is there at least 1 gallery picture? - Hence the count(pictureID)

3 - is there at least 1 private picture? - Hence the count(pictureID)

 

 

 

SELECT pictureLink FROM userPictures, 

(SELECT count(pictureID) FROM userPictures WHERE userID = 11 AND relType = 1 AND pictureGallery = 1 LIMIT 1) as galCount, 

(SELECT count(pictureID) FROM userPictures WHERE userID = 11 AND relType = 1 AND picturePrivate = 1 LIMIT 1) as priCount 

WHERE userID = 11 AND relType = 1 AND pictureMain = 1 LIMIT 1

 

Link to comment
Share on other sites

Okay, so let me rephrase it and see if I get you right.

 

What you want to do is to get the main picture for a certain user. Also, to check whether a user has a gallery picture or not, and whether a user has a private picture or not, am I correct?

 

We can use UNION here instead.

 

NOTE: For pictureMain, there should be only one row for this so you wont need to use LIMIT since the result then would be ambiguous. Also, you do not need LIMIT for COUNT since the result then would only be one.

 

SELECT 'mainpic' as descr, 
pictureLink as setoff
FROM userPictures as setA
WHERE userID = 11 
AND relType = 1 
AND pictureMain = 1
UNION 
SELECT 'gallery' as descr, 
COUNT(pictureID) as setoff
FROM userPictures as setB
WHERE userID = 11 
AND relType = 1 
AND pictureGallery = 1
UNION 
SELECT 'private' as descr, 
COUNT(pictureID) as setoff
FROM userPictures as setC
WHERE userID = 11 
AND relType = 1 
AND picturePrivate = 1

 

cheers,

 

Jay

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.