1
I have two tables in MYSQL:
account:
id apelido_usuario
1 caio
2 manoel
3 josé
product
idconta status
1 3
1 3
1 1
2 3
3 2
I made a SELECT like this:
SELECT * FROM conta
WHERE apelido_usuario <> ''
AND conta.id IN (SELECT produto.idconta FROM produto WHERE produto.status = '3');
It worked as wanted, it only lists users who have registered product and that the status of this product is equal to 3.
Now I need an ORDER BY, which is ordered by the user who has more products with the status = '3' in front, how to do?
I tried something like this:
SELECT * FROM conta
WHERE apelido_usuario <> ''
AND conta.id IN (SELECT produto.idconta FROM produto WHERE produto.status = '3')
ORDER BY SUM(conta.id IN (SELECT produto.idconta FROM produto WHERE produto.status = '3')) DESC;
But I realized it’s not right...
Just for the record, my query is much more complex than this, it has some LEFT JOIN, but that I omitted because I don’t think it comes to the case.
– caiocafardo