多表查询 根据结果跳转不同的页面

baibaip2 2011-07-15 09:19:19
select * from A where name like '%a%'
union all
select * from B where name like '%a%'
union all
select * from C where name like '%a%'
union all
select * from D where name like '%a%'

查询的结果应该是4个表里所有标题与a相似的结果
在结果中如何知道是哪个表里取出来的呢
比如 搜索出来的 absolute alick ckafdkl 这样的结果 不知道哪条属于哪个表

这么做的原因是我要做一个搜索的功能 被搜索的表有N个分别连接不同的页面
比如 查询表A 返回结果 页面跳转至新闻页面 查询表B 返回结果 页面跳转至产品页面
...全文
81 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
guotongyu1 2011-07-15
  • 打赏
  • 举报
回复
按照你说的 新闻页面 产品页面 ==总结出 这四张表应该是4个模块操作,你将四个表的查出的数据耦合到一个字段里,这是给你自己添麻烦。
xuexiaodong2009 2011-07-15
  • 打赏
  • 举报
回复
不使用union all返回四个表不行吗
或者添加一个字段

select *  ,1 as atable from A where name like '%a%'
union all
select * ,2 as atablefrom B where name like '%a%'
union all
select * ,3 as atable from C where name like '%a%'
union all
select * ,4 as atable from D where name like '%a%'
TimZhuFaith 2011-07-15
  • 打赏
  • 举报
回复
分别判断就是了。。。为什么写在一起?四个表字段一样么
deepmist 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 baibaip2 的回复:]
我现在已经实现好了的 就是分别判断的 但是读取速度慢 想优化一下
[/Quote]
不要select * ,取关键字段
baibaip2 2011-07-15
  • 打赏
  • 举报
回复
我现在已经实现好了的 就是分别判断的 但是读取速度慢 想优化一下
lrforever1 2011-07-15
  • 打赏
  • 举报
回复
随便加一个字段就行了,只要自己方便判断就Ok了
xrongzhen 2011-07-15
  • 打赏
  • 举报
回复

select * ,'A' as Source from A where name like '%a%'
union all
select * ,'B' as Source from B where name like '%a%'
union all
select * ,'C' as Source from C where name like '%a%'
union all
select * 'D' as Source from D where name like '%a%'

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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