Here’s my SQL (MySQL) command:
SELECT pp.pluginid
FROM plugins pp
WHERE
(pp.limit=-1)
OR
(pp.limit>(select count(a.pluginid) from ads a where a.pluginid = pp.pluginid))
ORDER BY count(a.pluginid)
The last line (order by) doesn’t work, and I understand why (because I don’t output this value from a subquery). But I don’t know how, and don’t know what command to look for.
Fixed!!!
Here’s what I did:
SELECT pp.pluginid, (select count(a.pluginid) from ads a where a.pluginid = pp.pluginid) as bb
FROM plugins pp
WHERE
(pp.limit=-1)
OR
(pp.limit>(select count(a.pluginid) from ads a where a.pluginid = pp.pluginid))
ORDER BY bb
Thanks!
Try using a join and a group by, like this:
Note that you need to use
HAVINGin place ofWHEREbecause the condition uses an aggregation.Here is a link to a demo on sqlfiddle.