views:

3846

answers:

1

First.. here are the two tables I've created (sans irrelevant columns)..

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;

Some background.. these tables hold data for a competitive ladder where players are compared against each other on an ordered standings by points. users_history1 is a table that contains records stored from previous seasons. users_ladders1 contains records from the current season. I'm trying to create a page on my site where players are ranked on the average points of their previous records and current record. Here is the main standings for a 1v1 ladder: http://vilegaming.com/league.x/standings1/3

I want to select from the database from the two tables an ordered list players depending on their average points from their users_ladders1 and users_history1 records. I really have no idea how to select from two tables in one query, but I'll try, as generic as possible, to illustrate it..

Using hyphens throughout the examples since SO renders it weird.

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

Needs the GROUP BY since some players may have played in multiple previous seasons.

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

I want to be able to combine both tables in a query so I can get the data in form of rows ordered by total points, and if possible also divide the total points by the number of unique records for the player so I can get the average.

+2  A: 

You'll want to use a 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

The sub query is the important part. It will give you a single table with the results of both the current and history tables combined. You can then select from that table and do COUNT and SUM to get your averages.

My MySQL syntax is quite rusty, so please excuse it. I haven't had a chance to run this, so I'm not even sure if it executes, but it should be enough to get you started.

sgriffinusa
yes, the UNION approach should do the trick!
tharkun
This works great, sgriffinusa!I do, however, have a small issue. If I try to eliminate any player that has only 1 record from both tables combined, then it gives me an error.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 pWHERE pcount>1GROUP BY p.idWhat I've add are the "AS pcount" and "WHERE pcount>1" parts. I get this error in phpMyAdmin's error report:Unknown column 'pcount' in 'where clause'
krissauquillo
MySQL doesn't like using aggregate functions in a where clause. From the MySQL 5 standard: "The HAVING clause can refer to aggregate functions, which the WHERE clause cannot." Using the HAVING keyword instead: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 > 1http://dev.mysql.com/doc/refman/5.1/en/select.htmlhttp://lists.evolt.org/archive/Week-of-Mon-20010813/055259.html
sgriffinusa