
正文
sqlserver的交集,sqlserver交叉连接
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
sqlserver 两表交集之外的其中一表数据
只用数据某一字段进行子查询是行不通的,因为你有三个字段为联合主腱,如果acode=bcode并不能保证abbrq=bbbrq和ay=by.
正确的方法是左外连接,如果左表a有右表b所没有的任何记录,该记录右表b任何字段为空
select a.* from tableA a left join tableB b on a.acode=b.bcode and a.abbrq=b.bbbrq and a.ay=b.by where b.bcode is null
相关问答
Q1: sqlserver 查询范围外的交集号
--整个约束就可以了
--创建表时创建约束
create table t1(col1 int not null check(col1 =50 and col1 = 90
and col1 =201 and col1 = 299))
--为已知表添加约束
alter table t1
add constraint CK_cardID check (
col1 =50 and col1 = 90
and col1 =201 and col1 = 299
)
--不明白可以随时问我 希望采纳
Q2: sqlserver 的交叉连接和内部连接有什么区别吗?
假设有两张表,A表和B表,A表有m条记录,x个属性;B表有n条记录,y个属性
交叉连接(cross join):A表和B表交叉连接就是,A表中的每条记录都和B表的的记录进行连接。A表和B表交叉最后会得到一个表会有m×n条记录,属性会有x+y个。而且这种连接比较消耗资源。
内部连接: 它是交叉连接的一个变形,内部连接一般都会有一个连接条件,只有满足条件的连接才会被选中。如果内部连接没有设置合理的条件,那它和交叉连接是等价的。一般得到的表会是=m×n条记录,=x+y个属性。
Q3: sql 查询统计部分有交集
sqlserver、oracle、db2下均可:
with t1(id,cardid,money,code,date) as (
select 1,1, 500, 10001, '13-12-1'
union all select 2,2, 400, 10001, '13-12-1'
union all select 3,1, 400, 0, '13-12-1'
union all select 4,1, 300, 0, '13-12-1'
union all select 5,1, 500, 10001, '13-12-1'
union all select 6,1, 600, 0, '13-12-1'
),
t2 as (select * from t1 where code!=0)
select t2.id, t2.cardid, t2.money, t2.code, t2.date, sum(t1.money) sum
from t1 join t2
on t1.cardid=t2.cardid and t1.id=t2.id
and not exists(select 1 from t2 t3
where t3.cardid = t2.cardid and t3.id=t1.id and t3.idt2.id)
group by t2.id, t2.cardid, t2.money, t2.code, t2.date
Q4: 请教一个比较难缠的SQL查询,同一个表在不确定值的情况下,挑出重复数量等于一定个数的值
我根据要求写了以下的SQL,不知道符不符合要求,
建表SQL
create table ta(
id number,
name varchar2(10)
)
insert into ta (ID, NAME)values (1, 'a');
insert into ta (ID, NAME)values (1, 'b');
insert into ta (ID, NAME)values (1, 'c');
insert into ta (ID, NAME)values (1, 'd');
insert into ta (ID, NAME)values (1, 'e');
insert into ta (ID, NAME)values (2, 'a');
insert into ta (ID, NAME)values (2, 'b');
insert into ta (ID, NAME)values (2, 'c');
insert into ta (ID, NAME)values (2, 'd');
insert into ta (ID, NAME)values (3, 'a');
insert into ta (ID, NAME)values (3, 'b');
insert into ta (ID, NAME)values (3, 'c');
insert into ta (ID, NAME)values (4, 'b');
insert into ta (ID, NAME)values (4, 'b');
insert into ta (ID, NAME)values (4, 'd');
以下是查询B列重复最少3次a和3次b的交集123,
with a as
(select *
from (select a.name, count(*) cs
from ta a
group by a.name
having count(*) 1) x
where x.cs = 3
and x.name in ('a', 'b'))
select *
from (select h.id, count(*) ww
from ta h
where h.name in (select name from a)
group by h.id) w
where ww 1
下面这个是结果是 1234
with a as
(select *
from (select a.name, count(*) cs
from ta a
group by a.name
having count(*) 1) x
where x.cs = 3
and x.name in ('a', 'b'))
select *
from (select h.id, count(*) ww
from ta h
where h.name in (select name from a)
group by h.id) w
感觉不太对,
另外那个
问题原话(要达到的要求,如果我要挑出字段B中至少三个相同的字段A的值(以B字段的abc值为例,要求取出字段A的列表为1,2,3、或者另一个bcd值去查出,1,2,4、这样出来的最终结果会是1,2,3,4)
前面的A出现3次的列为123没错,b出现最少3次的是1234,交集是 123
你这里的BCD是啥子意思,是B列三个特定的值 ???
Q5: 求多个表交集的SQL语句是什么呀???
使用 EXISTS 和 NOT EXISTS 查找交集与差集
使用 EXISTS 和 NOT EXISTS 引入的子查询可用于两种集合原理的操作:交集与差集。两个集合的交集包含同时属于两个原集合的所有元素。差集包含只属于两个集合中的第一个集合的元素。
city 列中 authors 和 publishers 的交集是作者和出版商共同居住的城市的集合。
USE pubs
SELECT DISTINCT city
FROM authors
WHERE EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)
下面是结果集:
city
--------
Berkeley
(1 row(s) affected)
当然,该查询可以写成一个简单的联接。
USE pubs
SELECT DISTINCT authors.city
FROM authors INNER JOIN publishers
ON authors.city = publishers.city
city 列中 authors 和 publishers 的差集是作者所居住的、但没有出版商居住的所有城市的集合,也就是除 Berkeley 以外的所有城市。
USE pubs
SELECT DISTINCT city
FROM authors
WHERE NOT EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)
该查询也可以写成:
USE pubs
SELECT DISTINCT city
FROM authors
WHERE city NOT IN
(SELECT city
FROM publishers)







