简体   繁体   中英

Slightly Complicated SQL query

Write an SQL query that returns number of articles by user, as the following results with the column header: 'user_id', 'username', 'total_count_of_articles' order by number of articles在此处输入图片说明

Something like this should return the specified resultset:

 SELECT u.id AS user_id
      , u.user_name
      , SUM(a.user_id IS NOT NULL) AS total_count_of_articles
   FROM users u
   LEFT
   JOIN users_articles a
     ON a.user_id = u.id
  GROUP BY u.id, u.user_name
  ORDER BY total_count_of_articles DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM