简体   繁体   English

如何从一个表中选择行并按另一表中一列的总和对其进行排序?

[英]How to select rows from one table and order it by the sum of a column from another table?

In MySQL, I have two tables. 在MySQL中,我有两个表。 First is members. 首先是会员。 Second is member_experience. 其次是member_experience。 The member_experience table contains values of all the XP that members earn and what they get it for. member_experience表包含成员赚取的所有XP的值以及获取的价格。 I want to write a query that selects members and orders results by the total of their xp. 我想编写一个查询,以选择成员并按其xp的总数对结果进行排序。 The query that I have written doesn't return any results. 我编写的查询未返回任何结果。

CREATE TABLE IF NOT EXISTS `members` (
  `member_id` int(15) NOT NULL AUTO_INCREMENT,
  `group_id` int(15) NOT NULL,
  `display_name` text NOT NULL,
  `email_address` text NOT NULL,
  `password` text NOT NULL,
  `status` tinyint(1) NOT NULL,
  `activation_code` varchar(16) NOT NULL,
  `date_joined` text NOT NULL
  PRIMARY KEY (`member_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `member_experience` (
  `experience_id` int(15) NOT NULL AUTO_INCREMENT,
  `member_id` int(15) NOT NULL,
  `value` mediumint(6) NOT NULL,
  `description` text NOT NULL,
  `date_earned` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`experience_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

This is my current query: 这是我当前的查询:

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)
       ORDER BY total_xp";

This will do the stuff: 这将完成以下工作:

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

try this: 尝试这个:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个表中选择并按另一表的列排序 - Select from one table and order by column of another table MySQL:如何从一个表中选择和显示所有行,并计算另一个表中where子句的总和? - MySQL: How to select and display ALL rows from one table, and calculate the sum of a where clause on another table? Mysql查询选择其中一个表中的总和与另一个表中的行总和进行比较 - Mysql Query select where the sum from one table is compared to the sum of rows in another table 如何根据另一个表列从一个表中选择一列? - how to select a column from one table according to another table column? 从一个表中选择多个列,然后作为行插入到另一个表中 - select multiple column from one table and insert into another as rows 如何根据一个表中的值选择另一个表中的行并对其排序? - How can I select and order rows from one table based on values contained in another? 如何使存储过程根据另一个表中的行顺序更新一个表中的一列? - How can I make a stored procedure to update one column in one table based on the order of rows from another table? 我如何从 MySQL 表中的 select 行按一列分组,另一列具有所需值 - How do I select rows from a MySQL table grouped by one column with required values in another INSERT从一个表到另一个表的列的行块 - INSERT block of rows from a column from one table to another table 如何从一个表中选择一列,从另一个表中选择另一列 - How to select a column from a table and another column from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM