SQL 查询问题

小土bibox 2020-05-29 05:21:37
有table1数据如下

row col
1 3
2 4
2 3

有table2数据如下

limit1 limit2
2 3

需要这样查询:
根据limit1 过滤 row 根据limit2过滤col,
根据查询过滤掉 第三条记录,查询出结构如下

row col
1 3
2 4

SQL语句怎么写
类似这样,只有两个都符合才过滤掉。
with as li (select limit1,limit2 from table2)
select * from table1 where row+'-'+col not in (select limit1+'-'+limit2 from li)
...全文
107 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhz_dxm 2020-05-30
  • 打赏
  • 举报
回复
可以在两个表分别加两个计算字段,直接通过计算字段进行过滤,性能也会高很多
唐诗三百首 2020-05-29
  • 打赏
  • 举报
回复

create table table1(row int,col int)

insert into table1(row,col)
 select 1,3 union all
 select 2,4 union all
 select 2,3

create table table2(limit1 int,limit2 int)

insert into table2(limit1,limit2)
 select 2,3


-- 方法1
select * 
 from table1 t1
 where not exists(select 1 
                  from table2 t2 
                  where t2.limit1=t1.row 
                  and t2.limit2=t1.col)

/*
row         col
----------- -----------
1           3
2           4

(2 行受影响)
*/


-- 方法2
select t1.* 
 from table1 t1
 left join table2 t2 on t1.row=t2.limit1
                        and t1.col=t2.limit2
 where t2.limit1 is null

/*
row         col
----------- -----------
1           3
2           4

(2 行受影响)
*/
RINK_1 2020-05-29
  • 打赏
  • 举报
回复


CREATE TABLE #T1
(ROW INT,
 COL INT)

INSERT INTO #T1
SELECT 1,3 UNION ALL
SELECT 2,4 UNION ALL
SELECT 2,3

GO 

CREATE TABLE #T2
(LIMIT1 INT,
 LIMIT2 INT)

INSERT INTO #T2
SELECT 2,3 UNION ALL
SELECT 1,3

GO

SELECT * FROM #T1 A
WHERE NOT EXISTS (SELECT 1 FROM #T2 WHERE A.ROW=LIMIT1 AND A.COL=LIMIT2)

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧