Merge 2 tables for a SELECT query?

前端 未结 3 1707
独厮守ぢ
独厮守ぢ 2021-01-03 07:42

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

CREATE TABLE users_history1 (
  circuit tinyint(1) unsigned NOT NULL default \'0\',         


        
3条回答
  •  别那么骄傲
    2021-01-03 08:09

    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.

提交回复
热议问题