Introduction:
Why introduce this problem, in the program encountered such a problem, in a data table, the same aid (Project ID) is added to the data table multiple times, so corresponding to a different time, just want to choose which record to add the earliest time.
Reference: Red and Black Alliance
Data tables used:
The effect you want to achieve:
Explanation: The same ID, added by different people (Name), we picked the lowest value (Val) of the row record.
Here's how:
Method One:
1 Select from T1 A, 2 (Select as Val from T1 Group by id) b3 where a.id=b.id and a.val=b.val
Method Two:
1 Select from T1 a 2 INNER JOIN3 (Select as from T1 Group by id) b 4 On A.id=b.id and A.val=b.val
Note: Method One and method two function the same, and there is no difference, but where is an implicit link, inner join is an explicit link, where is gradually marginalized.
Method Three:
1 Select from T1 a 2 where a.val= (selectfromwhere id=a.id)
With the same ID, take the minimum Val value.
Method Four:
1 Select from as where NOT EXISTS (selectfromwhere b.val<a.val and b.id=a.id)
Expand: Or Group By field, select the data that appears first:
The effect you want to achieve:
Method:
Select from as where val= (select1fromwhere a.id=b.id)
SQL GROUP By field, select the row record where the most value of the other field is located