我是sql的新手,任何人都可以帮我从内部查询中获取有序数据
select
*
from emp
where id
(select order from department where name = 'testing' order by order asc).
我从内部查询获取订单数据,并且我得到了id,我应该从emp表中获得与内部查询相同的顺序。谁能帮我这个。 TIA。
答案 0 :(得分:2)
如果将查询重新编写为连接是合理的:
select e.*
from emp e
inner join department d
on e.id = d.order
where d.name = 'testing'
order by d.order asc
如果department
中有多个行与order
中的一行或多行匹配emp
,那么这会改变您的结果 - 在这种情况下 this < / em> query将返回多行,而原始则不会。但如果这种情况不适用,那么这就是我建议的查询。
答案 1 :(得分:1)
如果emp.id和department.order之间的匹配次数超过1,这将给出正确的行数
select * from emp e
where exists
(select 1 from department d where d.name = 'testing'
and e.id = d.order1) -- order is a reserved word in several sql languages
order by id
你的桌子之间似乎有些搞笑。部门是否会包含有关emp的任何信息(我假设它是员工表)?
答案 2 :(得分:0)
无法保证存在具有内部查询的实际临时表,并且以某种方式对其进行排序和处理。但是,您可以通过emp.id对外部查询进行排序。
select * from emp where id in
(select order from department where name = 'testing')
order by id asc
答案 3 :(得分:0)
select *
from emp
where id in (select order from department where name = 'testing' order by order asc)
order by id asc
这可以帮助你
答案 4 :(得分:0)
select e.* from emp as e, department d where e.id=d.order and d.name='testing' order by d.order