0

I have the 2 following Queries:

This table can have multiple lines per user (many photos) and I just want to return the number/count of photos:

SELECT COUNT(*) FROM photos WHERE photo_description_profanity=1 AND photo_visible=1 AND photo_verified=1 AND userid='1000000002';

This table(s) is only one per user:

SELECT a.userid,a.profile_username,a.profile_gender,a.photo_name,a.photo_verified,b.profile_headline,
       YEAR(DATE_SUB(NOW(), INTERVAL TO_DAYS(a.profile_birthdate) DAY)) AS age,d.city,c.english AS country
FROM login AS a
JOIN `profiles` AS b ON a.userid=b.userid 
JOIN geoCountry AS c ON a.profile_country=c.countryCode
JOIN geoWorld AS d ON a.profile_geo_location=d.pid
WHERE a.userid IN ('1000000002','1000000003','1000000004');

Is it possible to combine these 2 queries?

This is at my limit of ability so any advice would be great :) thx

1
  • Please show what you want the results to look like. Commented Jan 29, 2014 at 1:36

3 Answers 3

1
SELECT
    a.userid,
    a.profile_username,
    a.profile_gender,
    a.photo_name,
    a.photo_verified,
    b.profile_headline,
    YEAR(DATE_SUB(NOW(), 
    INTERVAL TO_DAYS(a.profile_birthdate) DAY)) AS age,
    d.city,c.english AS country,
    (SELECT COUNT(*) FROM photos WHERE photo_description_profanity=1 AND photo_visible=1 AND photo_verified=1 AND userid= a.userid) AS result_count
FROM login AS a
JOIN `profiles` AS b ON a.userid=b.userid 
JOIN geoCountry AS c ON a.profile_country=c.countryCode
JOIN geoWorld AS d ON a.profile_geo_location=d.pid
WHERE a.userid IN ('1000000002','1000000003','1000000004');
Sign up to request clarification or add additional context in comments.

1 Comment

Hey thx so much... works great... didn't know I could add a select/query into a select like that... will remember... appreciated :)
1

It is possible. this takes two steps:

  • adapt the COUNT(*) into a filtering SUM()
  • plug the source table into the query

.

SELECT a.userid,a.profile_username,a.profile_gender,a.photo_name,a.photo_verified,b.profile_headline,
       YEAR(DATE_SUB(NOW(), INTERVAL TO_DAYS(a.profile_birthdate) DAY)) AS age,d.city,c.english AS country

//Here we insert the count column
,IFNULL(SUM(IF(photo_description_profanity=1 AND photo_visible=1 AND photo_verified=1,1,0)),0) AS photocount

FROM login AS a
JOIN `profiles` AS b ON a.userid=b.userid 
JOIN geoCountry AS c ON a.profile_country=c.countryCode
JOIN geoWorld AS d ON a.profile_geo_location=d.pid

//Here we insert the source table
LEFT JOIN photos ON photos.userid=a.userid

WHERE a.userid IN ('1000000002','1000000003','1000000004')

//Group by users
GROUP BY a.userid

2 Comments

This will return one row in MySQL.
@GordonLinoff Thanks! GROUP BY section escaped my mouse in copy-paste mode.
1

If you want this as an additional column, then cross join it into the results:

select const.cnt, . . . 
from (SELECT COUNT(*) as cnt
      FROM photos
      WHERE photo_description_profanity=1 AND photo_visible=1 AND
            photo_verified=1 AND userid='1000000002'
     ) const cross join
     login AS a
     JOIN `profiles` AS b ON a.userid=b.userid 
     JOIN geoCountry AS c ON a.profile_country=c.countryCode
     JOIN geoWorld AS d ON a.profile_geo_location=d.pid
//Here we insert the source table
     LEFT JOIN photos ON photos.userid=a.userid
WHERE a.userid IN ('1000000002','1000000003','1000000004');

The advantage of using a cross join instead of a nested select query is performance. MySQL will execute the nested select for every row in the return set. In the from clause, it will only be executed once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.