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 的語句, 關聯情況
(1)自然聯接:
SELECT 列名稱
FROM 表1 INNER 表2 ON 列1=列2
ORDER BY 列1 DESC 列2 DESC
(2)內聯接:
SELECT 列名稱
FROM 表1 INNER 表2 ON 列1=列2
(3)外聯接:
①左外聯接:
SELECT 列名稱
FROM 表1 LEFT JOIN 表2 ON 列1=列2
WHERE (列1='列內容') AND (列2='列內容')
ORDER BY 列1 ASC 列2 ASC
②右外聯接:
SELECT 列名稱
FROM 表1 RIGHT JOIN 表2 ON 列1=列2
WHERE (列1='列內容') AND (列2='列內容')
ORDER BY 列1 ASC 列2 ASC