[英]MySql Return Distinct Rows order by sno DESC group by column
I have the following mysql table: 我有以下mysql表:
Here in this table there could be houndred of thousands of rows, in which some may be with the same fir_sno [column No. 2]. 在此表中,这里可能有成千上万的行,其中有些行可能具有相同的fir_sno [第2列]。 My problem is that to select only the distinct rows from this table order by sno desc order.
我的问题是按sno desc顺序仅从此表顺序中选择不同的行。 And it should return sno : 2, 3. for this i have tried the following sql query but its return row no: 1,3 where i need row no:2, 3.
它应该返回sno:2,3。为此,我尝试了以下sql查询,但返回的行号是:1,3,其中我需要行号:2,3。
SELECT DISTINCT p.sno,p.fir_sno,p.short_order,p.date_fixed,
CONCAT(f.complainant,' V/S ',f.accused) AS title,f.under_sections,f.case_FIR_no
FROM pre_trials p, fir f
WHERE f.sno = p.fir_sno
GROUP BY p.fir_sno
ORDER BY p.sno DESC
Please help me how to do my required job? 请帮我怎么做我需要的工作?
Thanks for your precious time.l 感谢您的宝贵时间。
您可以检查WHERE f.sno <> p.fir_sno
You are grouping but no aggregate function is being used 您正在分组,但未使用任何聚合函数
Consider this similar example 考虑这个类似的例子
mysql> create table test (a int, b int, c int);
Query OK, 0 rows affected (0,47 sec)
mysql> insert into test values (1,1,1),(2,1,2),(3,2,3);
Query OK, 3 rows affected (0,08 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into test values (1,1,1),(2,1,2),(3,2,3);
mysql> select a, b, c from test group by b;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 1 | 1 |
| 3 | 2 | 3 |
+------+------+------+
2 rows in set (0,00 sec)
What you need, if I understand your question correctly, is to specify an aggregate on column a (in your example sno) in order to get only the last (highest) value per group on column b: 如果我正确理解了您的问题,那么您需要在a列上指定一个汇总(在您的示例中为sno),以便仅获得b列中每个组的最后一个(最高)值:
mysql> select max(a), b, c from test group by b;
+--------+------+------+
| max(a) | b | c |
+--------+------+------+
| 2 | 1 | 1 |
| 3 | 2 | 3 |
+--------+------+------+
2 rows in set (0,04 sec)
NOTE: This approach will not work in other DBMS as MySQL has a more relaxed group by syntax. 注意:这种方法在其他DBMS中将不起作用,因为MySQL的语法分组更为宽松。 For reference: http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
供参考: http : //dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.