跨資料庫連表查詢sql語句怎麼寫

時間 2021-10-14 22:25:42

1樓:射手幽靈伊

通過fdepartmentid查詢出部門名稱departname,這個部門名稱在a庫上也有了吧,假設部門名稱存在a庫的表t_dept中。

select a.fdepartmentid,b.departname,c.departid

from a..t_emp a join a..t_dept b on a.fdepartmentid = b.fdepartmentid

join b..depart c on b.departname = c.departname

2樓:

這樣應該符合你要求:

select fdepartmentid,departname from a..t_emp,opendatasource('sqloledb','data source=資料庫b的ip;user id=sa; password=資料庫b密碼').資料庫..

dept

where fdepartmentid = departid---或者寫你需要的條件

遠端資料庫操作:opendatasource('sqloledb','data source=資料庫b的ip;user id=sa; password=資料庫b密碼').資料庫..

dept

3樓:匿名使用者

select * from sbofl..table1 left join sbofk..table2 on ...

sbofl和sbofk代表兩個資料庫

mysql 多表聯合查詢語句怎麼寫

4樓:愛可生雲資料庫

table 語句

具體語法:table table_name [order by column_name] [limit number [offset number]]

其實從語法上看,可以排序,也可以過濾記錄集,不過比較簡單,沒有 select 那麼強大。

示例 1

簡單的建一張很小的表 y1,記錄數為 10 條。表 t1,插入 10 條記錄

mysql-(ytt/3305)->create table t1 (r1 int,r2 int);

query ok, 0 rows affected (0.02 sec)

mysql-(ytt/3305)->insert into t1

with recursive aa(a,b) as (

select 1,1

union all

select a+1,ceil(rand()*20) from aa where a < 10

) select * from aa;

query ok, 10 rows affected (0.00 sec)

records: 10  duplicates: 0  warnings: 0

簡單全表掃描mysql-(ytt/3305)->select * from t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)

table 結果mysql-(ytt/3305)->table t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)

看下 table 的執行計劃mysql-(ytt/3305)->explain table t1 order by r1 limit 2\g*************************** 1. row ***************************           id: 1  select_type:

******        table: t1   partitions: null         type:

allpossible_keys: null          key: null      key_len:

null          ref: null         rows: 10     filtered:

100.00        extra: using filesort1 row in set, 1 warning (0.

00 sec)

其實可以看到 table 內部被 mysql 轉換為 select 了。mysql-(ytt/3305)->show warnings\g*************************** 1. row ***************************  level:

note   code: 1003message: /* select#1 */ select `ytt`.

`t1`.`r1` as `r1`,`ytt`.`t1`.

`r2` as `r2` from `ytt`.`t1` order by `ytt`.`t1`.

`r1` limit 21 row in set (0.00 sec)

那其實從上面簡單的例子可以看到 table 在內部被轉成了普通的 select 來處理。示例 2應用於子查詢裡的子表。這裡要注意,內表的欄位數量必須和外表過濾的欄位數量一致。

克隆表 t1 結構mysql-(ytt/3305)->create table t2 like t1;query ok, 0 rows affected (0.02 sec)

克隆表 t1 資料mysql-(ytt/3305)->insert into t2 table t1;query ok, 10 rows affected (0.00 sec)records: 10  duplicates:

0  warnings: 0

table t1 被當做內表,表 t1 有兩個欄位,必須同時滿足 t2 檢索時過濾的欄位也是兩個。mysql-(ytt/3305)->select * from t2 where (r1,r2) in (table t1);+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)

注意:這裡如果過濾的欄位數量和子表數量不一致,則會報錯。

SQL資料庫查詢問題怎麼用這個語句

如果只是兩個表,可以用union 把這些資料都查出來。select 列0,列1 from dbo.1 5where 列1 10327177 union select 列0,列1 from dbo.35wwhere 列1 10327177 如果有更新其他表,而這些表有一定的規律性,就是可以用迴圈 動態...

資料庫SQL語句

create function dbo isin string1 varchar 100 string2 varchar 4000 returns bitas begin if charindex string1 string2 0 begin return 1 endreturn 0 end呼叫 ...

資料庫sql查詢語句,關於班級資訊,如下 學生名字,編號,班級,成績,把每個班級最大成績前20名,列出來

張琳玲兒 若為oracle資料庫,則使用rownom來進行查詢select from select from 班級資訊 order by 成績 desc where rownom 20 推薦答案oracle資料庫那個sql語句有問題,查出來的還是表裡面前20條資料,只是把這20條資料給降序排列了,而...