免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2075 | 回复: 4
打印 上一主题 下一主题

请教 主表与明细表查询问题!(菜鸟学习) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-05 10:35 |只看该作者 |倒序浏览
主表
tableOne  有字段
tableOne_id(pk), total_price,total_tax, dept_id,dept_name
明细表
tableTwo 有字段
tableTwo_id(pk),tableOne_id, project_name,material_name,material_id

pk表示 主键
请教如何 根据 project_name查询 tableOne的信息


select  total_price,total_tax, dept_id,dept_name from tableOne,tableTwo t
where tableOne.tableOne_id = t.tableOne_id and t.project_name = '项目'
这条可以查询 主表 有相关明细的情况
如何能 当明细表数据没有的情况也能 查出来
比如 tableOne_id  的值 为 100, 但是 tableTwo中 却没有 tableOne_id为100的数据,这样的话 就查不出来数据了,
希望通过一条SQL语句来查询 以上两种情况 (1. 根据project_name查询主表信息 2. 当主表没有相关明细时根据主表ID能查询出记录)

谢谢, 菜鸟学习中

论坛徽章:
0
2 [报告]
发表于 2006-07-05 10:53 |只看该作者
没太看懂你的问题,但估计是外部联接问题,可以用
select  total_price,total_tax, dept_id,dept_name from tableOne,
(select * from tableTwo  where   project_name = '项目') t where
tableOne.tableOne_id(+)=t.tableOne_id;
试试看

论坛徽章:
0
3 [报告]
发表于 2006-07-05 11:00 |只看该作者
呵呵,写的不清楚,我的意思是 前面的SQL 要固定, 而查询条件 在后面 加
也就是where   后面
可能 是根据 tableOne_id 查询,也有可能是根据 project_name,查询,
需要注意的是, 只要当 主表中tableOne_id有这样的值就要能查到,
而 明细表中 project_name 有值就能根据它查到相关主表的值

论坛徽章:
0
4 [报告]
发表于 2006-07-05 11:13 |只看该作者
不明白.如果只要主表有,select就有结果,那project_name有什么用

论坛徽章:
0
5 [报告]
发表于 2006-07-05 11:21 |只看该作者
主表
tableOne_id      total_price  total_tax  dept_id   dept_name
1                         100            12            011         部门1
2                         150          13              012         部门2

明细表  
tableTwo_id     tableOne_id      project_name
01                        1                    项目1

如以上数据
通过project_name='项目1' 查询时能查出tableOne_id 等于1 的主表的数据
通过tableOne_id ='2' 查询时能查出 主表的数据
以上两个查询通过一条SQL语句实现
查询条件在  WHERE 后面增加
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP