dongque1646 2015-02-14 08:42
浏览 35
已采纳

如何组合MySQL表

I have a database called 'mylist'. In it tables exists as 'list1' 'list2' 'list3'. All tables contain same structure and column names however data is different.

If I have to select all data from all tables then what query should I run ?

  • 写回答

1条回答 默认 最新

  • dqzuo0327 2015-02-14 08:50
    关注

    Try using UNION among all of them to get all the rows like below:

    SELECT output.mycol1, output.mycol2
    FROM
    (SELECT list1.mycol1, list1.mycol2
    FROM list1
    UNION
    SELECT list2.mycol1, list2.mycol2
    FROM list2
    UNION
    SELECT list3.mycol1, list3.mycol2
    FROM list3) AS "output"
    ORDER BY output.mycol1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?