To obtain the minimum value for each Myql MyQuery group?

Asked 2 years ago, Updated 2 years ago, 37 views

Hi, everyone. I'm trying to get the minimum value for each group, but I don't know well, so I'm uploading it here.

-----------------
  Name 번호 Number 날짜 Date
─────────────────────────
 Test 1 1 1 2017 2017-04-17
─────────────────────────
 Test 1 2 2 2017 2017-04-15
─────────────────────────
 Test 2 3 3 2017 2017-04-16
─────────────────────────
 Test 2 4 4 2017 2017-04-14
─────────────────────────
 Test 3 5 5 2017 2017-04-13
─────────────────────────
 Test 3 66 2017 2017-04-17
─────────────────────────

The result I want with that is

-----------------
  Name 번호 Number 날짜 Date
─────────────────────────
 Test 1 2 2 2017 2017-04-15
─────────────────────────
 Test 2 4 4 2017 2017-04-14
─────────────────────────
 Test 3 5 5 2017 2017-04-13
─────────────────────────

I want to get numbers 2, 4, and 5 after deduplicating the name like this, finding the minimum date. "T" I'm not sure how to get the minimum value for the query.

Group by only gets the maximum value, so is there a way to get the minimum value? <

php mysql

2022-09-22 14:17

2 Answers

Do the following:

SELECT T1.Name, T1.Date, T1.Number
FROM TABLE AS T1
INNER JOIN 
   (SELECT name, MIN (date) as date
     FROM Table
     GROUP BY NAME
   ) AS T2
ON T1. Name = T2.Name
    AND T1.Date = T2. Date


2022-09-22 14:17

SELECT Name, MIN (date), Number
FROM Table   
GROUP BY NAME;  


2022-09-22 14:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.