【问题标题】:Select In query how to order in where clause racleSelect In 查询如何在 where 子句 racle 中排序
【发布时间】:2014-12-15 19:33:03
【问题描述】:

我有两张桌子。

这是我的第一个查询

select teacher_id from teacher a , stats s where s.type=25 and a.teacher_id=s.teacherP_id order by s.count desc;

我的第二个查询

select *from teacher where teacher_id IN (select teacherP_id from stats where type = 25);

在我的 java 类中,我使用的是休眠

如果我使用第一个查询,我可以获取教师表,因为如果我使用 select,我只是重新调整teacher_id * 我正在获取统计信息和教师的所有字段。

如果我使用第二个查询,我无法按计数字段排序。

如何通过一次查询获得教师的所有字段并按统计计数字段排序。

【问题讨论】:

    标签: sql oracle hibernate


    【解决方案1】:

    请尝试,如果您可以进行以下查询:

    select a.* from teacher a , stats s where s.type=25 and a.teacher_id=s.teacherP_id order by s.count desc;

    【讨论】:

      【解决方案2】:

      首先,您应该学习明确的join 语法。如果你正在学习 SQL,一个简单的规则:不要在 from 子句中使用逗号。

      其次,您可以使用<table alias>.* 从表中选择所有列。以下是您要的查询:

      select t.*
      from teacher t join
           stats s
           on t.teach_id = s.teacherP_id
      where s.type = 25 
      order by s.count desc;
      

      【讨论】:

      • 感谢 Gordon 的帮助,表别名对您来说是什么意思?
      • ts 是表别名。这些是用于有时需要的表的缩写,但通常有助于使查询更易于编写和阅读。
      猜你喜欢
      • 2012-04-18
      • 2020-10-13
      • 2010-12-07
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多