这三个mysql查询语句能不能合并成一句sql语句???

程序登 2016-11-11 10:21:43
select id from engine_project a where a.project_name = #{projectName}

select wip from engine_machine a inner join engine_project b where b.id = a.project_id

select b.wip,b.create_date,b.type,b.id,b.acc,a.machine_name,a.machine_no,a.brand from engine_machine a inner join engine_location b where a.wip = b.wip and DATE_FORMAT(b.create_date,'%Y-%m-%d') > '2016-08-14'
...全文
248 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序登 2016-11-17
  • 打赏
  • 举报
回复
引用 7 楼 ch21st 的回复:
未测试

select a.id,b.wip,c.create_date,c.type,c.id,c.acc,b.machine_name,b.machine_no,b.brand 
from engine_project a 
inner join engine_machine b  on  a.id = b.project_id
inner join engine_location c on  c.wip = b.wip
where a.project_name = @projectName and  DATE_FORMAT(c.create_date,'%Y-%m-%d') > '2016-08-14' 

谢谢~ 我本来是这么写的,但是数据好像有错,然后我只好分开三句写了。就多了一些代码。
程序登 2016-11-11
  • 打赏
  • 举报
回复
引用 5 楼 yupeigu 的回复:
[quote=引用 3 楼 huang7511389 的回复:] [quote=引用 1 楼 yupeigu 的回复:] 没看明白,为什么要把3个语句合并成1个
因为一条sql语句会简单很多,减少很多代码。。[/quote] 但是第一个是单表,其他2个都是多表join的,数据肯定就不一样了,一对多会导致数据重复,所以不好合并。 建议,还是用简单的语句来实现,就算合并成功了,也会导致语句变慢的。[/quote] 原来如此,好的,我知道了
LongRui888 2016-11-11
  • 打赏
  • 举报
回复
引用 3 楼 huang7511389 的回复:
[quote=引用 1 楼 yupeigu 的回复:] 没看明白,为什么要把3个语句合并成1个
因为一条sql语句会简单很多,减少很多代码。。[/quote] 但是第一个是单表,其他2个都是多表join的,数据肯定就不一样了,一对多会导致数据重复,所以不好合并。 建议,还是用简单的语句来实现,就算合并成功了,也会导致语句变慢的。
程序登 2016-11-11
  • 打赏
  • 举报
回复
引用 2 楼 shoppo0505 的回复:
可以并成一个
我也觉得可以,但是大概怎么合并呢?
程序登 2016-11-11
  • 打赏
  • 举报
回复
引用 1 楼 yupeigu 的回复:
没看明白,为什么要把3个语句合并成1个
因为一条sql语句会简单很多,减少很多代码。。
shoppo0505 2016-11-11
  • 打赏
  • 举报
回复
可以并成一个
LongRui888 2016-11-11
  • 打赏
  • 举报
回复
没看明白,为什么要把3个语句合并成1个
道素 2016-11-11
  • 打赏
  • 举报
回复
未测试

select a.id,b.wip,c.create_date,c.type,c.id,c.acc,b.machine_name,b.machine_no,b.brand 
from engine_project a 
inner join engine_machine b  on  a.id = b.project_id
inner join engine_location c on  c.wip = b.wip
where a.project_name = @projectName and  DATE_FORMAT(c.create_date,'%Y-%m-%d') > '2016-08-14' 

22,210

社区成员

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

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