用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1724|回复: 3
收起左侧

[HELP] sql语句请教

[复制链接]
发表于 2014-5-15 19:05:53 | 显示全部楼层 |阅读模式
假设现在有一个如下的表
id    department    major    class
1            a               aa         a1
2            a               aa         a1
3            a               cc         a2
4            b               dd        b1
5            b               dd        b1
6            b               dd        b1
7            c               ee        c1
8            c               ee        c2
9            c               ff          c3
10          d               gg        d1
11          d               gg        d1

现在想要循环输出所有不重复的class,然后还有该class所对应的major和department,sql语句要怎么写?
期望输出如下
a               aa         a1
a               aa         a2
b               dd        b1
c               ee        c1
c               ee        c2
c               ff          c3
d               gg        d1
发表于 2014-5-15 20:53:21 | 显示全部楼层
本帖最后由 一叶扁舟 于 2014-5-15 21:12 编辑

每个class所对应的major和department是定值吗?
就是说每个相同的class对应的major和department是一样的

发表于 2014-5-15 21:15:15 | 显示全部楼层
select * from table where id in (select min(id) from table group by class)
试试这个吧
 楼主| 发表于 2014-5-16 09:28:10 | 显示全部楼层
一叶扁舟 发表于 2014-5-15 21:15
select * from table where id in (select min(id) from table group by class)
试试这个吧

太棒了,正是想要这样的效果,谢谢!但不知这样的语句用CI的AR应该如何去写呢?

本版积分规则