1 回答
TA贡献1786条经验 获得超11个赞
DECLARE @a TABLE(id INT ,aname VARCHAR(1),pric INT )
DECLARE @b TABLE(id INT ,aname VARCHAR(1),bname VARCHAR(1))
INSERT INTO @a VALUES(1,'a',5)
INSERT INTO @a VALUES(2,'b',2)
INSERT INTO @a VALUES(3,'c',5)
INSERT INTO @b VALUES(1,'a','b')
INSERT INTO @b VALUES(2,'a','a')
INSERT INTO @b VALUES(3,'a','b')
INSERT INTO @b VALUES(4,'b','c')
INSERT INTO @b VALUES(5,'b','c')
SELECT * FROM @a
SELECT * FROM @b
--以上是初始化数据
--下面是答案
SELECT [@a].* FROM @a
INNER JOIN (
SELECT [@a].aname,COUNT([@a].id) AS countName FROM @a
INNER JOIN @b ON [@a].aname = [@b].aname OR [@a].aname = [@b].bname
GROUP BY [@a].aname
)t ON t.aname=[@a].aname
WHERE [@a].pric - t.countName=1
不知道是不是你想要的答案
- 1 回答
- 0 关注
- 307 浏览
添加回答
举报