0

在 MySQL 中,我有两个表。首先是会员。其次是member_experience。member_experience 表包含成员获得的所有 XP 的值以及他们获得这些 XP 的目的。我想编写一个查询,根据他们的 xp 总数选择成员和订单结果。我写的查询没有返回任何结果。

如果不存在“成员”,则创建表(
  `member_id` int(15) NOT NULL AUTO_INCREMENT,
  `group_id` int(15) NOT NULL,
  `display_name` 文本不为空,
  `email_address` 文本不为空,
  `密码`文本不为空,
  `status` tinyint(1) NOT NULL,
  `activation_code` varchar(16) NOT NULL,
  `date_joined` 文本不为空
  主键(`member_id`)
) 引擎=InnoDB 默认字符集=latin1;

如果不存在 `member_experience` 则创建表(
  `experience_id` int(15) NOT NULL AUTO_INCREMENT,
  `member_id` int(15) NOT NULL,
  `value` mediumint(6) NOT NULL,
  `description` 文本不为空,
  `date_earned` 时间戳 NOT NULL DEFAULT '0000-00-00 00:00:00',
  主键(`experience_id`)
) 引擎=InnoDB 默认字符集=latin1;

这是我当前的查询:

       选择
          m.member_id,
          xp.member_id, SUM(xp.value) AS total_xp
       FROM 成员 AS m
       LEFT JOIN member_experience AS xp ON (xp.member_id = m.member_id)
       按 total_xp 排序";
4

2 回答 2

0

这将做的事情:

SELECT
  members.member_id,
  total_xp.sum_xp
FROM
  members
   LEFT JOIN
     (SELECT 
       member_id, 
       SUM(value) AS sum_xp 
     FROM 
       member_experience 
     GROUP BY 
       member_id) AS total_xp
     ON members.member_id = total_xp.member_id
ORDER BY
  total_xp.sum_xp
于 2013-08-28T07:17:29.960 回答
0

尝试这个:

SELECT
   m.member_id,
   xp.member_id, SUM(xp.value) AS total_xp
FROM 
   members AS m
LEFT JOIN 
   member_experience AS xp ON xp.member_id = m.member_id
GROUP BY 
   m.member_id
ORDER BY 
   SUM(xp.value)
于 2013-08-28T07:22:57.920 回答