注册 登录
编程论坛 ASP技术论坛

[求助]多表查询问题

dushui265 发布于 2007-10-28 14:34, 794 次点击

现有多个表table1,table2,table3,他们中的字段名全都相同
我想从这四个表中实现某一字段的模糊查询,如字段name
大家帮我看下,那里错了
sql="select 20 * from table1,table2,table3 where table1.name like '%"&request.form("content")&"%'"
and table2.name like '%"&request.form("content")&"%'" and table3.name like '%"&request.form("content")&"%'"

14 回复
#2
永夜的极光2007-10-28 15:43
sql="select top 20 * from table1,table2,table3 where table1.name like '%" & request.form("content") & "%'
and table2.name like '%" & request.form("content") & "%' and table3.name like '%" & request.form("content") & "%'"
#3
dushui2652007-10-28 15:58
好像还是一样啊,总是出现大量重复的记录?
#4
purana2007-10-28 16:17

当然了.因为产生的迪卡尔积了.

#5
tianyu1232007-10-28 17:09
sql="select 20 * from table1,table2,table3 where table1.name like '%"&request.form("content")&"%'"
and table2.name like '%"&request.form("content")&"%'" and table3.name like '%"&request.form("content")&"%'"

这三个表之间存在着关系吗?

如果存在:

后面加上相关联的字段

如:table1中的id与table2的id关联 table1中的id与table3的id关联

sql="select 20 * from table1,table2,table3 where table1.name like '%"&request.form("content")&"%'"
and table2.name like '%"&request.form("content")&"%'" and table3.name like '%"&request.form("content")&"%' and table1.id=table2.id and table1.id=table3.id"
#6
dushui2652007-10-28 19:36
三个表没有关系吧,都是有一样的字段,ID是自动编号,就好像是一个表,现在把它分成了两个表来存储,现在怎么来关联他们呢?
#7
madpbpl2007-10-28 20:04
楼主想达到什么样的效果呢?(就是你的搜索结果希望是怎么样呢?)
#8
purana2007-10-28 20:12
以下是引用dushui265在2007-10-28 19:36:05的发言:
三个表没有关系吧,都是有一样的字段,ID是自动编号,就好像是一个表,现在把它分成了两个表来存储,现在怎么来关联他们呢?

按你这么说.
那应该用union all了.
select f1,f2 from table1 where name ='xxx'
union all
select f1,f2 from table2 where name ='xxx'
union all
select f2,f2 from table3 where name ='xxx'

#9
madpbpl2007-10-28 20:35
以下是引用purana在2007-10-28 20:12:25的发言:

按你这么说.
那应该用union all了.
select f1,f2 from table1 where name ='xxx'
union all
select f1,f2 from table2 where name ='xxx'
union all
select f2,f2 from table3 where name ='xxx'

同意这个写法,我也想用union解决这个问题。
随便问下楼上的,你怎么变成男孩了?
见过你的贴子,还想向你请教sql server呢?

#10
purana2007-10-28 20:47
这里都是讨论学术研究的.性别不重要...
SQL Server我也是初学.很多东西也不懂.不过可以一起研讨..
#11
madpbpl2007-10-28 20:57
以下是引用purana在2007-10-28 20:47:25的发言:
这里都是讨论学术研究的.性别不重要...
SQL Server我也是初学.很多东西也不懂.不过可以一起研讨..

不要误会,没有别的意思。请见谅!
sql server我是一窍不通,最近正在学js,过几天去上海办点事,回来后开始做js练习,并开始学习sql server
,到时请多指教,谢谢!

#12
purana2007-10-28 21:03
呵呵.指教就说不上了.
我也只学了2个月..1个月MySQL
一起探讨吧.
#13
dushui2652007-10-30 08:23

在网站中把后台数据库由原来的Access转换为SQL的话,是不是只需修改连接数据库的conn文件就可以了?

#14
hmhz2007-10-30 09:14
Access转换为SQL 需要注意几个地方,时间要更改,ID自动编号要改,单选的bit false和true 要改成0和1
#15
dushui2652007-11-01 11:00

谢了

1