In oracle, the distinct record statement (without distinct) can only filter all records in the query field with the same distinct keyword (the record set is the same), but it does not work if you want to specify a field, in addition, distinct keywords are sorted with low efficiency.
Select distinct name from t1 can eliminate duplicate records, but only one field can be taken. Now, the value of id and name must be taken at the same time. Select distinct id, name from t1 can take multiple fields, but only records with the same values of the two fields can be eliminated. Therefore, distinct cannot achieve the desired effect, you can use group by to solve this problem. For example, the fields to be displayed at www.2cto.com Are A, B, and C. If the content of field A cannot be repeated, use the following statement: select A, min (B ), min (C), count (*) from [table] where [condition] group by Ahaving [condition] order by A desc in order to display the title header, select, min (B), min (C), count (*) for example select A as A, min (B) as B, min (C) as C, count (*) as repeat times
The displayed fields and sorting fields must be included in group by, but the displayed fields include min, max, count, avg, sum and other Aggregate functions can not include min (B), min (C), count (*) in the preceding clause in group (*) the general condition is written after where, and the condition with Aggregate functions is written after having. If having adds count (*)> 1. You can find records with repeat times greater than 1. If having plus count (*)> 2 in the previous sentence, you can find records with repeat times greater than 2 in record.
If having plus count (*)> = 1 in the preceding statement, all records can be queried, but only one record is displayed, in addition, the number of repetitions is displayed later-this is the expected result, and the statement below hibernate can be used to query the data that is duplicated: www.2cto.com select Field 1, Field 2, count (*) from table name group by field 1, Field 2 having count (*)> 1 change the> number above to = to query data that has no duplicates. For example, select count (*) from (select gcmc, gkrq, count (*) from gczbxx_zhao t group by gcmc, gkrq havingcount (*)> = 1 order by GKRQ) select * from gczbxx_zhao where viewid in (select max (viewid) from gczbxx_zhao group bygcmc) order by gkrq desc --- this is still feasible.