Howto put these two queries together?

Howto put these two queries together?

am 25.05.2006 23:47:08 von Benny Schudel

Howto put these two queries together?

sql = "SELECT DISTINCT medium_id, COUNT(tag) AS count FROM tags WHERE
tag = 'test' GROUP BY medium_id ORDER BY count DESC"

This query is working.

Id like to try something like

sql = "SELECT * FROM media WHERE id IN (SELECT DISTINCT medium_id FROM
tags WHERE tag = 'test')"

But how can I order the resut by quantity (counts of tag)?

Thank you for help.
//BEN

Re: Howto put these two queries together?

am 26.05.2006 01:16:34 von Bill Karwin

Benny Schudel wrote:
> Howto put these two queries together?
> sql = "SELECT DISTINCT medium_id, COUNT(tag) AS count FROM tags WHERE
> tag = 'test' GROUP BY medium_id ORDER BY count DESC"
> sql = "SELECT * FROM media WHERE id IN (SELECT DISTINCT medium_id FROM
> tags WHERE tag = 'test')"

SELECT m.*, COUNT(*) AS count
FROM media AS m JOIN tags AS t ON m.id = t.medium_id AND t.tag = 'test'
GROUP BY m.id
ORDER BY count DESC

Assuming m.id is a unique key for the media table.

Regards,
Bill K.