做一个模糊查询不知道怎么弄

rendehua380 2009-11-17 10:38:28
有4张表分别是基本费用、特殊费用、礼金费用、暂支费用、报销费用,它们之间没有关系,如何把它们符合条件的都显示出来呢?
...全文
135 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
sayoath 2009-11-17
  • 打赏
  • 举报
回复


--你联合时,需要字段数量相同,类型一样。
select
bPrice,
bPaymentTime,
bNumber,
Office.oName as OfficeName,
UserInfo.uName as UserName
from
BasicExpense left join Office
on
BasicExpense.bOfficeId=Office.oId --这里的联合条件应该给错了。
left join
UserInfo
on
BasicExpense.bUId=UserInfo.uId--
where
bPrice between 500 and 6000

union

select
sPrice,
sPaymentTime,
sNumber,
Office.oName as OfficeName,
UserInfo.uName as UserName
from
SpecialExpense left join Office
on
BasicExpense.bOfficeId=Office.oId --
left join
UserInfo
on
BasicExpense.bUId=UserInfo.uId--
where
sPrice between 500 and 6000

--你执行单条语句时,可以吗?


dotnet201 2009-11-17
  • 打赏
  • 举报
回复
嗯,不错,用union
rendehua380 2009-11-17
  • 打赏
  • 举报
回复
加了括号也没用
阿彪兄 2009-11-17
  • 打赏
  • 举报
回复
union吧
kasin000 2009-11-17
  • 打赏
  • 举报
回复

(
select bPrice,bPaymentTime,bNumber,Office.oName as OfficeName,UserInfo.uName as UserName
from BasicExpense left join Office on BasicExpense.bOfficeId=Office.oId left join UserInfo on BasicExpense.bUId=UserInfo.uId
where bPrice between 500 and 6000
)

UNION
(
select sPrice,sPaymentTime,sNumber,Office.oName as OfficeName,UserInfo.uName as UserName
from SpecialExpense
left join Office on BasicExpense.bOfficeId=Office.oId
left join UserInfo on BasicExpense.bUId=UserInfo.uId
where sPrice between 500 and 6000
)
是不是要括号????
rendehua380 2009-11-17
  • 打赏
  • 举报
回复

select bPrice,bPaymentTime,bNumber,Office.oName as OfficeName,UserInfo.uName as UserName
from BasicExpense left join Office on BasicExpense.bOfficeId=Office.oId left join UserInfo on BasicExpense.bUId=UserInfo.uId
where bPrice between 500 and 6000 UNION
select sPrice,sPaymentTime,sNumber,Office.oName as OfficeName,UserInfo.uName as UserName
from SpecialExpense left join Office on BasicExpense.bOfficeId=Office.oId left join UserInfo on BasicExpense.bUId=UserInfo.uId
where sPrice between 500 and 6000

这样写汇报无法绑定由多个部分组成的标识符 "BasicExpense.bOfficeId"。
无法绑定由多个部分组成的标识符 "BasicExpense.bUId"。
rendehua380 2009-11-17
  • 打赏
  • 举报
回复
UNION能不能跟left join一起使用吗
  • 打赏
  • 举报
回复
union all....
ivws_19 2009-11-17
  • 打赏
  • 举报
回复
没有一点联系的话那别人怎么知道该怎么查
哈哈 2009-11-17
  • 打赏
  • 举报
回复
select A,B,C from 基本费用 where 条件
   UNION
select A,B,C from 特殊费用 where 条件
   UNION
select A,B,C from 礼金费用 where 条件
   UNION
select A,B,C from 暂支费用 where 条件
   UNION
select A,B,C from 报销费用 where 条件
rendehua380 2009-11-17
  • 打赏
  • 举报
回复
没有啊,都是单独的表有几个共同的字段,金额、付款日期。。。
fuda_1985 2009-11-17
  • 打赏
  • 举报
回复
它们之间没有关系没有关系你不会创造你想要的关系么?
ivws_19 2009-11-17
  • 打赏
  • 举报
回复
没有外键关联么? 表之间总有点联系吧
  • 打赏
  • 举报
回复
like
xuyang09 2009-11-17
  • 打赏
  • 举报
回复
union all....还需要什么呢、。?
rendehua380 2009-11-17
  • 打赏
  • 举报
回复
是可以用空代替 谢谢美女啊
rainy816 2009-11-17
  • 打赏
  • 举报
回复
14楼说的对~~
sayoath 2009-11-17
  • 打赏
  • 举报
回复
你还是把表结构和需要的返回的结果贴出来吧!
sayoath 2009-11-17
  • 打赏
  • 举报
回复
解决:

字段类型不同,可以进行转换。
数量不同可以拿空项补

select Name,Age from Employee union select Name,'' as Age from Users

类型都是以第一条语句为准则,UNION 的语句,字段类型得与第一条语句类型匹配。


rendehua380 2009-11-17
  • 打赏
  • 举报
回复
union查询的字段各表之间都相同不然查不了,这样也满足不了我现在的情况
加载更多回复(1)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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