简体   繁体   English

如何从SQL获取不同的行

[英]How to get distinct rows from SQL

This is my SQL table. 这是我的SQL表。 here i want to get all 在这里,我希望得到所有

       marks_table
ID  STUD_ID MARKS   VERSION    VERIFICATION_ID
1      50     90       1             2
2      22     50       1             2
3      33     20       1             2
4      10     30       1             2
5      55     50       1             2
6      55     40       2             2
7      20     60       1             2
8      30     90       1             2
9      10     88       1             3
10     10     45       2             3

What i want is, get all the result by verification_id and version is greater values. 我想要的是,通过verification_id得到所有结果,版本是更大的值。 For example ID 5,6 and 9,10 have same stud_id with different marks and there version is also diferent. 例如,ID 5,6和9,10具有相同的stud_id,具有不同的标记,并且版本也不同。 I want to get max version result and all other result from that verification_id. 我想获得最大版本结果以及来自该verification_id的所有其他结果。

In CodeIgniter i have used following commands. 在CodeIgniter中,我使用了以下命令。

$this->db->select('*');
$this->db->from('marks_table');
$this->db->where('version IN (SELECT MAX(version) FROM marks_table)',NULL,FALSE);
$this->db->where('verification_id','2');
$this->db->get();

What i got is only final max version 我得到的只是最终的最终版本

   marks_table
    ID  STUD_ID MARKS   VERSION    VERIFICATION_ID
    6      55     40       2             2

What i really want, like this 我真正想要的是这样的

       marks_table
ID  STUD_ID MARKS   VERSION    VERIFICATION_ID
1      50     90       1             2
2      22     50       1             2
3      33     20       1             2
4      10     30       1             2
6      55     40       2             2
7      20     60       1             2
8      30     90       1             2

First find the Max version then fetch it's data: 首先找到Max版本,然后获取它的数据:

   select * from marks_table a
    where version = (select max(version) from 
                        marks_table where stud_id = a.stud_id);

I have modified the srini.venigalla answer. 我修改了srini.venigalla的答案。 Thanks to him. 谢谢他。

  select * from marks_table a
    where version = (select max(version) from 
                        marks_table where stud_id = a.stud_id) AND VERIFICATION_ID = 2;

Try to get multiple rows from table 尝试从表中获取多行

$this->db->select('*');
$this->db->from('marks_table');
$this->db->where('version IN (SELECT MAX(version) FROM marks_table)',NULL,FALSE);
$this->db->where('verification_id','2');
$this->db->get()->result_array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM