简体   繁体   中英

Users with most comments from Drupal + phpBB

i need to get a list of users with mosts comments + posts from Drupal + phpBB.

I am using mySQL.

Structure of Drupal database: dr_comments(cid,uid) dr_users(uid,name)

phpBB: phpbb_posts(post_id,poster_id) phpbb_users(user_id,username)

I have SQL code for users with most comments from Drupal and phpBB not together.

Drupal:

SELECT
 U.name,
 COUNT(C.cid) AS CommentCount
FROM
 dr_users AS U
 INNER JOIN dr_comments AS C ON U.uid = C.uid
GROUP BY
 U.name
ORDER BY
 COUNT(C.cid) DESC

LIMIT 10

phpBB:

SELECT
 U.username,
 COUNT(C.post_id) AS CommentCount
FROM
 phpbb_users AS U
 INNER JOIN phpbb_posts AS C ON U.user_id = C.poster_id
GROUP BY
 U.username
ORDER BY
 COUNT(C.post_id) DESC

LIMIT 10

I dont know how to merge it together.

You can use UNION:

SELECT name, SUM(CommentCount) AS TotalCommentCount
FROM (
  SELECT U.name, COUNT(C.cid) AS CommentCount
  FROM dr_users AS U
  INNER JOIN dr_comments AS C ON U.uid = C.uid
  GROUP BY U.name

  UNION ALL

  SELECT U2.username AS name, COUNT(C2.post_id) AS CommentCount
  FROM phpbb_users AS U2
  INNER JOIN phpbb_posts AS C2 ON U2.user_id = C2.poster_id
  GROUP BY U2.username

) temp GROUP BY name

That should do its job. Note for the script above I expect that the usernames are on both programmes equal.

You can also check this SQL Fiddle .

Your ...merge them together... can be interpreted in several ways. If you want to simply union both resultsets keeping order of each resultset intact then you can do

(
  SELECT u.name,
         COUNT(c.cid) CommentCount
    FROM dr_users u JOIN dr_comments c
      ON u.uid = c.uid
   GROUP BY u.name
   ORDER BY COUNT(c.cid) DESC
   LIMIT 10
)
UNION ALL
(
  SELECT u.username,
         COUNT(C.post_id) CommentCount
    FROM phpbb_users u JOIN phpbb_posts c 
      ON u.user_id = c.poster_id
   GROUP BY u.username
   ORDER BY COUNT(c.post_id) DESC
   LIMIT 10
)

If you need to union both resultsets and then reorder them

(
  SELECT u.name,
         COUNT(c.cid) CommentCount
    FROM dr_users u  JOIN dr_comments c
      ON u.uid = c.uid
   GROUP BY u.name
   ORDER BY COUNT(c.cid) DESC
   LIMIT 10
)
UNION ALL
(
  SELECT u.username,
         COUNT(C.post_id) CommentCount
    FROM phpbb_users u JOIN phpbb_posts c 
      ON u.user_id = c.poster_id
   GROUP BY u.username
   ORDER BY COUNT(c.post_id) DESC
   LIMIT 10
)
   ORDER BY CommentCount DESC

It also might be interpreted as get TOP 10 from both phpbb and drupal

SELECT name, COUNT(cid) CommentCount
  FROM
(
  SELECT u.name, c.cid
    FROM dr_users u  JOIN dr_comments c
      ON u.uid = c.uid
   UNION ALL
  SELECT u.username, c.post_id
    FROM phpbb_users u JOIN phpbb_posts c 
      ON u.user_id = c.poster_id
) q
   GROUP BY name
   ORDER BY CommentCount DESC
   LIMIT 10

Here is SQLFiddle demo

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