Merge 2 tables for a SELECT query?

前端 未结 3 1708
独厮守ぢ
独厮守ぢ 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:00
    Select col1 from test1 where id = '1'
    union 
    select * from table2 
    

    this one can also used for the joining to tables.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-03 08:19

    If you want to merge to table and you want to select particular column from one table and in another table want to select all.

    e.g.

    Table name = test1 , test2
    

    query:

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

    if you want to merge with particular column

    query:

    SELECT test1.column1,test1.column2, test2.* FROM test1 ,test2 where test2.column3='(what ever condition u want to pass)'
    
    0 讨论(0)
提交回复
热议问题