【问题标题】:Merge 2 tables for a SELECT query?为 SELECT 查询合并 2 个表?
【发布时间】:2010-11-10 21:47:49
【问题描述】:

首先..这是我创建的两个表(没有不相关的列)..

CREATE TABLE users_history1 (
  circuit tinyint(1) unsigned NOT NULL default '0',
  userh_season smallint(4) unsigned NOT NULL default '0',
  userh_userid int(11) unsigned NOT NULL default '0',
  userh_rank varchar(2) NOT NULL default 'D',
  userh_wins int(11) NOT NULL default '0',
  userh_losses int(11) NOT NULL default '0',
  userh_points int(11) NOT NULL default '1000',
  KEY (circuit, userh_userid),
  KEY (userh_season)
) ENGINE=MyISAM;

CREATE TABLE users_ladders1 (
  circuit tinyint(1) unsigned NOT NULL default '0',
  userl_userid int(11) unsigned NOT NULL default '0',
  userl_rank char(2) NOT NULL default 'D',
  userl_wins smallint(3) NOT NULL default '0',
  userl_losses smallint(3) NOT NULL default '0',
  userl_points smallint(4) unsigned NOT NULL default '1000',
  PRIMARY KEY (circuit, userl_userid),
  KEY (userl_userid)
) ENGINE=MyISAM;

一些背景知识.. 这些表格包含竞争天梯的数据,在该天梯上,玩家在按积分排序的积分榜上进行比较。 users_history1 是一个表,其中包含以前季节存储的记录。 users_ladders1 包含当前季节的记录。我正在尝试在我的网站上创建一个页面,在该页面上,玩家根据他们以前的记录和当前记录的平均分进行排名。以下是 1v1 天梯的主要排名: http://vilegaming.com/league.x/standings1/3

我想从两个表中的数据库中选择一个有序列表玩家,这取决于他们从 users_ladders1 和 users_history1 记录中的平均分。我真的不知道如何在一个查询中从两个表中进行选择,但我会尝试尽可能通用地说明它..

在整个示例中使用连字符,因为 SO 会使它变得很奇怪。

SELECT userh-points 
FROM users-history1 
GROUP BY userh-userid 
ORDER BY (total userh-points for the user)

需要 GROUP BY,因为有些球员可能已经参加过多个赛季。

SELECT userl-points 
FROM users-ladders1 
ORDER BY userl-points

我希望能够在一个查询中组合这两个表,这样我就可以获得按总分排序的行形式的数据,如果可能的话,还可以将总分除以玩家的唯一记录数,这样我就可以取平均值。

【问题讨论】:

    标签: mysql database join merge


    【解决方案1】:

    您需要使用UNION SELECT

    SELECT p.id, COUNT(p.id), SUM(p.points)
    FROM (SELECT userh_userid AS id, userh_points AS points
          FROM users_history1
          UNION SELECT userl_userid, userl_points
          FROM users_ladders1) AS p
    GROUP BY p.id
    

    子查询是重要的部分。它将为您提供一个表,其中包含当前表和历史表的结果。然后,您可以从该表中进行选择并执行 COUNT 和 SUM 以获得平均值。

    我的 MySQL 语法很生疏,请见谅。我没有机会运行它,所以我什至不确定它是否执行,但它应该足以让你开始。

    【讨论】:

    • 这很好用,sgriffinusa!不过,我确实有一个小问题。如果我试图从两个表中消除任何只有 1 条记录的玩家,那么它会给我一个错误。 SELECT p.id, COUNT(p.id) AS pcount, SUM(p.points) FROM (SELECT userh_userid AS id, userh_points AS points FROM users_history1 UNION SELECT userl_userid, userl_points FROM users_ladders1) AS p WHERE pcount>1 GROUP BY p. id 我添加的是“AS pcount”和“WHERE pcount>1”部分。我在 phpMyAdmin 的错误报告中收到此错误:'where 子句'中的未知列'pcount'
    • MySQL 不喜欢在 where 子句中使用聚合函数。来自 MySQL 5 标准:“HAVING 子句可以引用聚合函数,而 WHERE 子句不能。”改用 HAVING 关键字:SELECT p.id, COUNT(p.id) AS pcount, SUM(p.points) FROM (SELECT userh_userid AS id, userh_points AS points FROM users_history1 UNION SELECT userl_userid, userl_points FROM users_ladders1) AS p GROUP BY p.id HAVING pcount > 1 dev.mysql.com/doc/refman/5.1/en/select.html lists.evolt.org/archive/Week-of-Mon-20010813/055259.html
    【解决方案2】:

    如果要合并到表中,并且要从一个表中选择特定列,而在另一个表中要全选。

    例如

    Table name = test1 , test2
    

    查询:

    SELECT test1.column1,test1.column2, test2.* FROM test1 ,test2
    

    如果你想与特定列合并

    查询:

    SELECT test1.column1,test1.column2, test2.* FROM test1 ,test2 where test2.column3='(what ever condition u want to pass)'
    

    【讨论】:

      【解决方案3】:
      Select col1 from test1 where id = '1'
      union 
      select * from table2 
      

      这个也可以用来连接表格。

      【讨论】:

      • 多表查询最常用的是JOIN。有多个 JOIN 选项的作用不同,因此请阅读所有选项。
      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多