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 多表關聯 求和語句 怎么寫
你結果顯示有問題吧,最后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多個關聯表求和語句
設查詢客戶ID為'A0001',查詢語句如下:
select
a.客戶ID,
a.項目ID,
sum(a.合同數量) as 該項目總的合同數量,
sum(b.完成數量) as 該項目總的完成數量
from tb2 a,tb3 b
where a.項目ID=b.項目ID and a.客戶ID='A0001'
group by a.客戶ID,a.項目ID
如果還要包含其他字段:代碼如下:
select
c.客戶ID,c.項目ID,c.該項目總的合同數量,c.該項目總的完成數量,d.其他字段
from
(select a.客戶ID,a.項目ID,sum(a.合同數量) as 該項目總的合同數量, sum(b.完成數量) as 該項目總的完成數量 from tb2 a,tb3 b where a.項目ID=b.項目ID and a.客戶ID='A0001' group by a.客戶ID,a.項目ID ) c,tb1 d
where
c.客戶ID=d.客戶ID
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語句:多表關聯查詢
在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
這樣,就把四個表關聯起來查詢了。如果有更多的表,可以一個一個的關聯下去,不過我還是不希望關聯的表太多.