关于数据库的排序问题

来源:百度知道 编辑:UC知道 时间:2024/07/04 12:01:03
问题是这样的,有两张表:table1,table2
table1:
id name PaiXuZiDuan
123112 张三 1
222222 李四 2

table2:
id table1ID core
123123 222222 80
444444 123112 90

查询:select * from table2;
但是要求查询结果用table1里面的PaiXuZiDuan排序,有办法吗?
谢谢
傲世绝情泪,我是按PaiXuZiDuan字段排序,并非ID,而且要求输出结果只能为table2的字段,所以不能用组合查询

我想要HEBERNET的HQL语句,注:HQL不支持*

先添加主键:
alter table table1
add constraint 主键名 primary key (id )
再添加外键:
alter table table2
add constraint 外键名 foreign key (id ) references table1(id)

最后运行
select *
from table1 inner join table2
on table1.id=table2.id
order by table1.PaiXuZiDuan /*这样就可以了*/

查询:select * from table2;
但是要求查询结果用table1里面的PaiXuZiDuan排序,有办法吗?

将两张表连接啊,用inner join on ....

select table2.* from table2 inner join table1 on table2.table1ID=table1.id order by table1.PaiXuZiDuan

select a.* from table2 a, table1 b where a.id = b.id order by b.PaiXuZiDuan

===========================
select * from table2 a ,table1 b where a.table1ID = b.id order by b.PaiXuZiDuan
===========================
这种查询肯定是要关联查询的。