如何优化编写这样表结构的查询语句!?????(急)

acheng 2003-06-09 03:58:24
表一:用户基本信息表

字段
userid 用户id (唯一值,主健)
username 用户名
birthday 出生年月 (格式:1999-01-12)
reg_time 注册时间

表二:教育信息表

字段
userid 用户id (对应表一userid , 重复)
e_name 教育机构
s_time 开始时间
e_time 结束时间

表三:工作经验表

字段
userid 用户id (对应表一userid , 重复)
w_name 公司名称
s_time 开始时间
e_time 结束时间

组合查询表单:

用户名:
年龄: 岁至 岁 (输入格式: 25 岁至 30岁)
教育机构:
公司名称:

注意:如何进行年龄的判断,因为表一的格式是1999-01-12

怎样优化编写组合查询sql语句???????
...全文
84 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
superscreen 2003-07-05
  • 打赏
  • 举报
回复
mark
hjb111 2003-06-10
  • 打赏
  • 举报
回复
select distinct a.username,datediff(year,a.birthday,b.s_time) as s_time,datediff(year,a.birthday,b.e_time) as e_time,b.e_name
from 表一 a ,表二 b
where a.userid=b.userid
union all
select distinct c.username,datediff(year,c.birthday,d.s_time) as s_time,datediff(year,c.birthday,d.e_time) as e_time,d.w_name
from 表一 c ,表三 d
where c.userid=d.userid
acheng 2003-06-10
  • 打赏
  • 举报
回复
myflok(阿棋) Happiness(乐乐) happydreamer(小黑)

各位:

查询出来的记录都是重复的,例如:

10001 乐乐 。。。
10001 乐乐 。。。

怎样写令查询出来的记录不重复?
dafu71 2003-06-09
  • 打赏
  • 举报
回复
select 表1.username,datediff(yy,birthday,getdate()) age,表2.e_name,表3.w_name from
表1 left join 表2 on 表1.userid=表2.userid left join 表3 on 表1.userid=表3.userid
sunwo 2003-06-09
  • 打赏
  • 举报
回复
我认为你的这种组合查询结果没有实际意义,因为罗列出的数据太罗嗦。
Happiness 2003-06-09
  • 打赏
  • 举报
回复
select a.username,datediff(year,a.birthday,b.s_time) as s_time,datediff(year,a.birthday,b.e_time) as e_time,b.e_name,null as w_name
from 表一 a ,表二 b
where a.userid=b.userid
union all
select c.username,datediff(year,c.birthday,d.s_time) as s_time,datediff(year,c.birthday,d.e_time) as e_time,null as e_name,d.w_name
from 表一 c ,表三 d
where c.userid=d.userid
Happiness 2003-06-09
  • 打赏
  • 举报
回复
select a.username,datediff(year,a.birthday,b.s_time) as s_time,datediff(year,a.birthday,b.e_time) as e_time,b.e_name
from 表一 a ,表二 b
where a.userid=b.userid
union all
select c.username,datediff(year,c.birthday,d.s_time) as s_time,datediff(year,c.birthday,d.e_time) as e_time,d.w_name
from 表一 c ,表三 d
where c.userid=d.userid

happydreamer 2003-06-09
  • 打赏
  • 举报
回复
SELECT a.username,datediff(yy, a.birthday ,getdate()) AS 年龄 b.e_name,c.w_name
FROM 用户基本信息表 a JOIN 教育信息表 b ON a.userid=b.userid
JOIN 工作经验表 c ON a.userid=c.userid
WHERE datediff(yy, a.birthday ,getdate()) BETWEEN 25 AND 30
wedding123 2003-06-09
  • 打赏
  • 举报
回复
查询很容易解决,关键是如果各表中记录超过20万条后,一个查询可能要2分钟以上。
其实提高速度的方法很简单,建立相应的聚集INDEX及非聚集INDEX即可,查询时间在
2秒左右。
acheng 2003-06-09
  • 打赏
  • 举报
回复
myflok(阿棋) Happiness(乐乐) happydreamer(小黑)

各位:

可以不可以利用存储过程提高速度?存储过程如何写? 还有可以不可以建一个中间视图??

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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