0

I have a problem with a query, I hope you can help me. I have two tables.

Users

id_user | name | date

Publications

id_publication | publication | date | id_user

I want the first results (users) are the ones who have published

I have this query.

SELECT * FROM users WHERE (name LIKE  '%" . $word . "%') ORDER BY (SELECT id_user, count(*) FROM publications GROUP BY id_user) DESC

But I not have the desired result

i want a query in form of search where print all users and order by the number of publications. For example.. if in table user this the name : Maria, Angela. And Maria have more publications, the first to appear is the name of Maria.

thanks for reading

2 Answers 2

0

You can join both of the tables as follows to get the desired result:

SELECT *
FROM Users
LEFT JOIN
(SELECT id_user, COUNT(Publication) AS PubCount
 FROM Publications
 GROUP BY id_user
) as T
ON T.id_user = Users.is_User
ORDER BY T.PubCount DESC
2
  • thanks Guranjuan Singh for answering! I worked the query Majid Feb 27, 2015 at 23:26
  • @user3462094 if you find answers to be helpful, please accept them and/or vote them up. Mar 2, 2015 at 19:43
0
SELECT * FROM users us WHERE (name LIKE '%" . $word . "%') 
ORDER BY (SELECT count(*) FROM publications pub WHERE us.id_user = pub.id_user) DESC
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.