SQL statement to select from 2 different tables, from two different databases (same server)

后端 未结 3 1429
难免孤独
难免孤独 2021-01-21 00:38

How do I select from multiple tables in different databases on the same server?

Also, Is there a way to have an identifying marker so I could see where the results came

3条回答
  •  耶瑟儿~
    2021-01-21 00:53

    Doing a union seems like your best bet here. A union will combine the results of two queries.

    select name, 'table1' as fromTbl
    from db1.schema.table1
    where name like '%j%'
    
    union --or union all depending on what you want
    
    select name, 'table2' as fromTbl
    from db2.schema.table2
    where name like '%j%'
    

提交回复
热议问题