34,590
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE Node (ID INT,NAME VARCHAR(10),TYPE VARCHAR(10))
INSERT Node
SELECT '1','001','A' UNION ALL
SELECT '2','002','B' UNION ALL
SELECT '3','003','A'
CREATE TABLE Node_Company(ID INT,Company VARCHAR(10))
INSERT Node_Company
SELECT '1','CPY1'UNION ALL
SELECT '1','CPY2'UNION ALL
SELECT '2','CPY1'
1.
SELECT DISTINCT [name],[type],company
FROM Node_Company a left join Node b
ON A.ID=B.ID
--WHERE Type='A'
ORDER BY B.TYPE
name type company
---------- ---------- ----------
001 A CPY1
001 A CPY2
002 B CPY1
(3 行受影响)
2.
SELECT DISTINCT [name],[type],company
FROM Node a left join Node_Company b
ON A.ID=B.ID
--WHERE Type='A'
ORDER BY A.TYPE
name type company
---------- ---------- ----------
001 A CPY1
001 A CPY2
003 A NULL
002 B CPY1
(4 行受影响)
create table nod (id int,name varchar(100),type varchar(100))
insert nod
select '1','001','A'union all
select '2','002','B'union all
select '3','003','A'
create table nod_company(id int,company varchar(100))
insert nod_company
select '1','cpy1'union all
select '1','cpy2'union all
select '2','cpy1'
select distinct a.name,a.type,b.company from nod a left join nod_company b on a.id=b.id order by a.type
第一种用内联
select a.name,a.type,b.company from node a inner join node_company b on a.id=b.id
第二种左连
select a.name,a.type,b.company from node a left outer join node_company b on a.id=b.id
1.
select a.name,a.type,b.company from
node a left join node_company b
on a.id=b.id
2.
select a.name,a.type,b.company from
node a join node_company b
on a.id=b.id