sql联合查询后如何标记字段是属于原来哪个表的?

canzai 2008-06-09 09:39:24
有表1和表2

结构如下:
表1:

id name sex old
1 王小兵 男 23
2 张进柑 女 27
3 张平平 女 11
4 李朝困 男 85


表2:
id name hei old
1 王天二 169 15
2 张婷婷 164 24
3 李浇波 175 28
4 张朝阳 173 33

==================================
我要实现结果是把两张表合在一起查询,按old从大到小排序,显示name字段,但显示时要根据原来表中的ID号加上超链接(表1的用aa.asp ,表2的用bb.asp)
如:
<a href="aa.asp?id=4">李朝困</a>
<a href="bb.asp?id=4">张朝阳</a>
<a href="bb.asp?id=3">李浇波</a>
<a href="aa.asp?id=2">张进柑</a>
<a href="bb.asp?id=2">张婷婷</a>
……

===============================================
我现在只会显示没有链接的,不懂得如何判断并根据每个姓名的ID选择用aa.asp 还是用bb.asp




...全文
201 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
木豆酒吧 2008-06-09
  • 打赏
  • 举报
回复
走挺专业的
如果你学习过 把表分开显示的话 那也可以

不过我忘记了不好意思
No_Data_Found 2008-06-09
  • 打赏
  • 举报
回复
注意 关键字 使用[]

循环记录集时

if rs("表名") = "表1" then url = "aa.asp"

if rs("表名") = "表2" then url = "bb.asp"

response.write "<a href='" & url & "?id=" & rs("id") &"'>" & rs("name") & "</a>"
xie_yanke 2008-06-09
  • 打赏
  • 举报
回复
select [id], [name], [sex], [old], '' as hei, 'aa.asp' as thisURL from 表1
union all
select [id], [name], '' as sex, [old], [hei], 'bb.asp' as thisURL from 表2
order by old desc

将不同的字段,补齐. 自定义一个字段为URL,根据结果调用thisURL即可知道是何表.
xie_yanke 2008-06-09
  • 打赏
  • 举报
回复
select [id], [name], [sex], [old], '' as hei, 'aa.asp' as thisURL from 表1
union all
select [id], [name], '' as sex, [old], [hei], 'bb.asp' as thisURL from 表2

将不同的字段,补齐. 自定义一个字段为URL,根据结果调用thisURL即可知道是何表.
No_Data_Found 2008-06-09
  • 打赏
  • 举报
回复
select 表名,id,name,old
from
(
select "表1" 表名,id,name,old from 表1
union
select "表2" 表名,id,name,old from 表2
)
order by old desc
canzai 2008-06-09
  • 打赏
  • 举报
回复
谢谢各位
xie_yanke 2008-06-09
  • 打赏
  • 举报
回复
显示多少字段,就取多少字段即可.
jhwcd 2008-06-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xie_yanke 的回复:]
select [id], [name], [sex], [old], '' as hei, 'aa.asp' as thisURL from 表1
union all
select [id], [name], '' as sex, [old], [hei], 'bb.asp' as thisURL from 表2
order by old desc

将不同的字段,补齐. 自定义一个字段为URL,根据结果调用thisURL即可知道是何表.
[/Quote]
就用3楼的方法吧。
老紫竹 2008-06-09
  • 打赏
  • 举报
回复
字段有 别名,而且推荐给字段加别名。
flush_520 2008-06-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xie_yanke 的回复:]
select [id], [name], [sex], [old], '' as hei, 'aa.asp' as thisURL from 表1
union all
select [id], [name], '' as sex, [old], [hei], 'bb.asp' as thisURL from 表2
order by old desc

将不同的字段,补齐. 自定义一个字段为URL,根据结果调用thisURL即可知道是何表.
[/Quote]
支持!
楼主说字段多,能达到50个字段不嘛?
写程序不要怕多,而要追求速度和以后维护,这样不只两个表,多少个表都可以.数据量大的时候速度也要相对快一点
canzai 2008-06-09
  • 打赏
  • 举报
回复
to: xie_yanke
我实际中两个表有很多不同的字段,总不能一个一个补齐吧,那岂不是很麻烦


to: songpengasp
我试了你的方法,总提示“语句未结束”,不知道是什么原因

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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