SQL表與另兩個表有關聯,SQL 一個表與另兩個表有關聯

2022-05-02 17:14:35 字數 4193 閱讀 9135

1樓:匿名使用者

有什麼問題呢,這樣結果難道不對嗎?

比如a表有資料

10,a,b

b表有資料

10,x1,y1

10,x2,y2

c表有資料

10,l1,m1

10,l2,m2

關聯後得到的結果是4條

10,a,b,x1,y1,l1,m1

10,a,b,x1,y1,l2,m2

10,a,b,x2,y2,l1,m1

10,a,b,x2,y2,l2,m2

你想得到什麼樣的結果呢?

2樓:張伊笙

select *

from

(select

a.a01, a.a02, a.a03, b.b02, b.b03from a join b

on a.a01=b.a01

where a.a01 > 10

) a,

(select

a.a01, a.a02, a.a03, c.c02, c.c03from a inner join c

on a.a01=c.a01

where a.a01 > 10) c

where a.a01=c.c01

sql語句:一個表和另外兩個表的關聯語句?

3樓:bolibei玻璃

select * from a inner join b on a.bid=b.id inner jion c on a.cid=c.id

select * from a,b,c where a.bid=b.id and a.cid=c.id

如果你是完成兩個表的關聯,那直接查詢就可以了;

如果你要完成一個表同時查詢,就可以使用給表起別名的辦法。

兩個不同的表的關聯查詢如下:

select *

from a,b

where 條件

同一個表的關聯查詢:

select *

from a a,

where a.*=a.*

sql語句:一個表和另外兩個表的關聯語句

4樓:匿名使用者

select * from a inner join b on a.bid=b.id inner jion c on a.cid=c.id

或者子要相互之間的關聯欄位相等就可以了,如

select * from a,b,c where a.bid=b.id and a.cid=c.id

5樓:匿名使用者

select test.*,test1.*,test2.*

from test1 left join test on test1.id=test.id

left join test2 on test.id=test2.id

sql中有關聯的兩個表,怎麼查出一個表中沒有對應到另一個表的資料?

6樓:匿名使用者

假定bai通du過zhiid欄位

dao關版聯權

1)select *

from a

where id not in (select id from b)2)select a.*

from a left join b on a.id = b.idwhere b.id is null

sql查詢一個表中兩個欄位對應的另一個表的資料,應該怎麼操作?

7樓:匿名使用者

根據 news表中的 news_type_id = 1 查出 news_type表中的 「透明點評」 這條資料,「透明點評」是最後需要查出來的位置資料。

子查詢或者表連線

比如表連線的方式就可以寫成:

select n.id,t.type_name,title from news as n inner join news_type as t onnn.

news_type_id=t.type_id;

只查「透明點評」的資料子查詢可以寫成:

select * from news where news_type_id=(select type_id from news_type where type_name='透明點評');

8樓:匿名使用者

sql查詢一個表中兩個欄位對應的另一個表的資料,可以通過如下操作進行:輸入語句:select a.

* from test a,test1 b where a.id=b.id and a.

name=b.name;

sql server,一張表中,有多個欄位關聯另一張表,怎麼寫sql語句?

9樓:匿名使用者

對於這個問題,無論語句怎麼寫,其實執行起來資料庫內部還是要用join操作來進行處理的,所以你用join就可以,優化的話可以再user表的id屬性上加索引(如果是主鍵預設是有索引的)

10樓:匿名使用者

原理的確是來

你那源個思路。bai但可以通du過下面zhi方式來簡化寫法

daoselect

auserid,auserdes = (select userdes from user b where a.auserid = b.user),

buserid,buserdes = (select userdes from user b where a.bauserid = b.user),

cuserid,cuserdes = (select userdes from user b where a.causerid = b.user),

from userlist a

sql server中 兩個不同的資料庫中的兩張表如何關聯? 10

11樓:小執著

1、首先就是建立幾個沒有任何關係的表,但是注意,你在將要作為外來鍵表的表上必須使用與將要作為主鍵表的資料型別相同。

2、將能唯一標識的那一行設定為主鍵,其他表類似。

3、接下來新增關係,如下圖。

4、拖動需要新增的關係,直接看圖。

5、關聯完成,如圖。

12樓:匿名使用者

一臺伺服器上的兩個不同的資料庫

如test1資料庫的a表和test2資料庫的b表

可以這樣寫

選定一個資料庫如test1

select 檢索欄位

from a

inner jion b on b.檢索欄位=a.檢索欄位

where

關聯條件

兩臺伺服器上的,2個資料庫

如果是跨伺服器的,需要建立 資料庫連結

首先建立連線伺服器

use master

goif exists (select * from sysservers where srvname = 'linkserver')

begin

exec sp_dropserver 'linkserver', 'droplogins'

endgo

exec sp_addlinkedserver 'linkserver','','sqloledb','0.0.0.0'

exec sp_addlinkedsrvlogin 'linkserver','false',null,'sa','command'

golinkserver是連線伺服器的名字,你自己喜歡取什麼就取什麼

0.0.0.0,sa,command。是你要連線的另外一臺資料庫的ip和登入使用者密碼

查詢命令:

select 檢索欄位

from a

inner jion linkserver.b on b.檢索欄位=a.檢索欄位

where

關聯條件

13樓:匿名使用者

在同一個伺服器。直接寫全名稱就可以關聯

不同伺服器。可以建立連線資料庫 然後關聯

14樓:

同一臺伺服器就好說了直接關聯查詢就可以了,不同的伺服器,需要使用opendatasource,

至於說的2005的資料庫連線跟2000的是一樣的,如果是預設例項安裝的話,如果新建的例項,需要在連線後面加例項名

怎麼把SQL多個表關聯合併成一個表

用union all group by。多少id都沒關係吧,除非你說的是有上千個表。select id,sum qty1 qty1,sum qty2 qty2,sum qty3 qty3 select id,qty1,0 qty2,0 qty3 from 表1 union all select id...

sql查詢兩個表中滿足某些條件的資料總數

如果欄位一樣的話,可以用union all,即select from 表1 where a,b,cunion all select from 表2 where a,b,c 假設兩張表分別為tab1,tab2 select sum t from select count 1 t from tab1 w...

sql中如何合併兩個特定的欄位,sql怎樣把兩個欄位合併成一個欄位顯示

1 建立模擬的資料表 create table goodscate mid int not null,code varchar 10 not null,name varchar 20 not null,2 新增模擬資料 insert into goodscate mid,code,name valu...