How to combine results from multiple tables with different columns?

前端 未结 5 1895
北恋
北恋 2020-12-16 05:42

I have several tables with different numbers and types of columns, and a single column in common.

+--------+---------+------------+-------------+
| person |          


        
相关标签:
5条回答
  • 2020-12-16 05:54

    Join on person....

    I.e.

    Select t1.(asterix), t2.(asterix) FROM beards t1 INNER JOIN moustaches t2 On t2.person = t1.person

    0 讨论(0)
  • 2020-12-16 05:58
    SELECT *
    FROM   beards
           JOIN moustaches
             ON moustaches.person = beards.person
    WHERE  person = "bob"  
    
    0 讨论(0)
  • 2020-12-16 06:00

    I had fun with this, not sure it's entirely manageable with what more you have to add, but it accomplished the goal.

    create table beard (
    person varchar(20)
    ,beardID int
    ,beardStyle varchar(20)
    ,beardLength int )
    
    create table moustache(
    person varchar(20)
    ,moustacheID int
    ,moustacheStyle varchar(20))
    
    
    insert into beard 
    select 'bob', 1, 'rasputin', 1
    union select 'bob', 2, 'samson', 12
    
    insert into moustache
    select 'bob', 1, 'fu manchu'
    
    declare @facialhair table (
    person varchar(20)
    ,beardID int
    ,beardStyle varchar(20)
    ,beardLength int
    ,moustacheID int
    ,moustacheStyle varchar(20))
    
    declare @i int
    declare @name varchar(20)
    
    set @name = 'bob'
    set @i = (select COUNT(*) from beard where person = @name)
            + (select COUNT(*) from moustache where person = @name) 
    
    print @i
    
    while @i > 0
        begin 
            insert into @facialhair (person, beardID, beardStyle, beardLength)
            select person, beardID, beardStyle, beardLength
            from beard
            where person = @name
        set @i = @i-@@ROWCOUNT
    
            insert into @facialhair (person, moustacheID, moustacheStyle)
            select person, moustacheID, moustacheStyle
            from moustache
            where person = @name
        set @i = @i-@@ROWCOUNT
        end
    
    select *
    from @facialhair
    
    0 讨论(0)
  • 2020-12-16 06:05

    this should be working fine:

    SELECT * FROM `beards` b LEFT OUTER JOIN `mustaches` ON (0) WHERE  person = "bob"
    UNION ALL
    SELECT * FROM `beards` b RIGHT OUTER JOIN `mustaches` ON (0) WHERE  person = "bob"
    

    you don't have to handle the columns by yourself. the left and right outer join do this job. unfortunately mysql doesn't have a full join. that's why you have to do it this way with a union

    SELECT * FROM `customer` b LEFT OUTER JOIN `charges` ON (0) LEFT OUTER JOIN `day` ON (0)
    UNION
    SELECT * FROM `customer` b RIGHT OUTER JOIN `charges` ON (0) LEFT OUTER JOIN `day` ON (0)
    UNION
    SELECT * FROM `customer` b LEFT OUTER JOIN `charges` ON (0) RIGHT OUTER JOIN `day` ON (0)
    

    this is a local test i made

    0 讨论(0)
  • 2020-12-16 06:17

    I think you would be better by making queries for data in each table.

    One of other possibilities is to concatenate data from all columns into one big string (you could choose some sign to separete column's values), then you should be able to use union all clause to combine results from each query - but then you will have to parse each row.. And data types will be lost.

    0 讨论(0)
提交回复
热议问题