Select from multiple tables in one call

前端 未结 7 1132
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 13:01

In my code I have a page that includes information from 3 different tables. To show this information I make 3 SQL select calls and unite them in one list to pass as Model to my

7条回答
  •  独厮守ぢ
    2021-02-13 13:44

    You can use UNION ALL to merge multiple queries.

    Do something like this:

    SELECT * FROM Table1
    UNION ALL
    SELECT * FROM Table2
    

    Edit:

    You can do this if you want to know where a single record is from:

    SELECT *, 1 AS TableName FROM Table1
    UNION ALL
    SELECT *, 2 AS TableName FROM Table2
    

    This will add another column that can be used to split the array into 3 lists.

提交回复
热议问题