这个查询语句那里不对了?

网络科技 2009-07-23 02:42:51

select * from table1 a,table2 b,table3 c where a.userid='xzy' or b.userid='xzy' or c.userid='xzy'

说明:我要在三个表中,查询看是否有userid值为"xzy"的记录,只要有任意一个表中userid值为"xzy",就算找到记录,
以上写法好像不行,我table1表中有userid值为"xzy"的记录,可查询结果还是为空,为什么呢?感觉这个也很简单,没想到写了查不到,请大虾们帮忙看下吧,谢了!
...全文
88 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
网络科技 2009-07-23
  • 打赏
  • 举报
回复
各位速度快啊,解决了,感谢各位帮忙,结了!
  • 打赏
  • 举报
回复

if object_id('table1') is not null
drop table table1
create table table1
(
userid varchar(10)
)

if object_id('table2') is not null
drop table table2
create table table2
(
userid varchar(10)
)

if object_id('table3') is not null
drop table table3
create table table3
(
userid varchar(10)
)

insert table1
select 'xzy'

insert table2
select 'abc'

insert table3
select 'ccc'


select * from table1 a,table2 b,table3 c where a.userid='xzy' or b.userid='xzy' or c.userid='xzy'

/**
userid userid userid
---------- ---------- ----------
xzy abc ccc

(所影响的行数为 1 行)
**/
子陌红尘 2009-07-23
  • 打赏
  • 举报
回复
或许楼主要的是这个效果:

select * from table1 where userid='xzy'
union all
select * from table2 where userid='xzy'
union all
select * from table3 where userid='xzy'
feixianxxx 2009-07-23
  • 打赏
  • 举报
回复
select * from table1 a
where a.userid='xzy'
union all
select * from table2 b
where b.userid='xzy'
union all
select * from table3 c
where c.userid='xzy'
dj3688 2009-07-23
  • 打赏
  • 举报
回复
顶钻钻~~
feixianxxx 2009-07-23
  • 打赏
  • 举报
回复
select * from table1 a
where a.userid='xzy'
union all
select * from table2 b
where b.userid='xzy'
union all
select * from table3 c
c.userid='xzy'

用union all 代替 OR 这样效率也高
子陌红尘 2009-07-23
  • 打赏
  • 举报
回复
select 
*
from
table1 a
full outer join
table2 b
full outer join
table3 c
where
isnull(a.userid,'')='xzy' or isnull(b.userid,'')='xzy' or isnull(c.userid,'')='xzy'
htl258_Tony 2009-07-23
  • 打赏
  • 举报
回复
like '%xzy%'
可能存在的内容不够精确吧.换用LIKE的试试.

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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