SQL語句-關聯查詢有兩張表:A,B表A中有數據:出庫ID,圖書 愛問知
select b.出庫ID,b.出庫總額 from A a,B b where a.出庫ID=b.出庫ID and b.出庫總額 not in ( select sum(a.圖書總價) from A where a.出庫ID in ( select distinct b.出庫ID from B where 1=1 ) )最里層的in 是找出所有不同的出庫ID,外面一層的in是查詢出這些出庫ID在A表中圖書總價的合計值,然后查詢出A,B兩表出庫ID相同,但B表的出庫總額不等于A表的圖書總價合計值的列。
sql語句三張表關聯查詢
Select a.*,*ature_alarm_gradient From ugp_check_node as a
Inner Join ugp_converge_node as b On *=*
Inner Join ugp_converge_node_config as c On *=*個人覺得,b里面應該是沒有id的,應該是b里面有個列與a相關另一個列與c相關,a與c沒關系,這樣才會出現3表關聯的需求,不然又不查b 那么這個查詢跟b就沒什么關系了
多表關聯,SQL語句查詢,求幫助
create table A(AID) --類型略
create table B(AID,EID)
create table C(CID)
create table D(CID,EID)
create table E(EID)
select * from E where EID in (select EID from B where AID in (select AID from A where AName='abc'))
and EID in (select EID from D where CID in (select CID from C where CName='xyz'))
sql 表的關聯語句 怎么寫
你結果顯示有問題吧,最后id=2怎么來的?
創建表:
create table table1
(id int,
類別 varchar(10),
貨號 varchar(3))
insert into table1 values (1,'電子','011')
insert into table1 values (2,'零件','022')
insert into table1 values (3,'主板','033')
create table table2
(id int,
貨號 varchar(3),
數量 int)
insert into table2 values (1,'011',5)
insert into table2 values (2,'022',6)
insert into table2 values (3,'033',-8)
insert into table2 values (4,'011',22)
insert into table2 values (5,'022',65)
insert into table2 values (6,'033',81)
查詢:
select *,a.類別,a.貨號,SUM(b.數量)
from table1 a,table2 b where a.貨號=b.貨號 and a.貨號='011'
group by *,a.類別,a.貨號
結果:
SQL語句:多表關聯查詢
在SQL里,常常需要對多個表關聯起來進行查詢,下面把我寫的一個簡單的多表關聯的例子給大家看看,方法很簡單,只要你學會原理就行:
select
* id,* oid,* number,*r seOrder,*ce endprice,--第一個表的字段
* uid,*ime oDatetime,--第二個表的字段
*e proname,* spec,*al material,* price,--第三個表的字段
*1 price1,*2 price2,*3 price3,*4 price4,*5 price5 --第四個表的字段
from
orderlist o --表一
left join products p on *=* --表二
left join orderForm d on *=* --表三
left join classify c on *=* --表四
--更多的表
order by * desc
這樣,就把四個表關聯起來查詢了。如果有更多的表,可以一個一個的關聯下去,不過我還是不希望關聯的表太多.