2

I am new for PHP and MYSQL. In my study I met a problem

I have a table like this:

+----+----------+---------+
| time | name   | number  |
+----+----------+---------+
| 12.1 | google | 10      |
| 12.2 | yahoo  | 15      |
| 12.3 | msn    | 20      |
| 12.1 | google | 10      |
| 12.1 | google | 29      |
| 12.2 | yahoo  | 10      |
+----+----------+---------+

but I want the talbe like this:

+----+----------+---------+
| time | name   | number  |
+----+----------+---------+
| 12.2 | yahoo  | 15      |
| 12.3 | msn    | 20      |
| 12.1 | google | 29      |
+----+----------+---------+

when the time and the name are the same, I want the row with the max number,

what should I do? I am very worrying about this problem and thank you for anwsing me

3
  • 1
    Where's your code to do that? We can help you with your code, but we can't write it for you Commented Mar 29, 2013 at 8:42
  • what you have tried so far? Commented Mar 29, 2013 at 8:43
  • but this can't return the one with max number Commented Mar 29, 2013 at 8:46

2 Answers 2

4

TRY ( not tested )

SELECT `time`, `name`, `number`
FROM tbl
GROUP BY `name`,`time`
HAVING MAX(`number`)

tip : TIME is a mysql reserve keyword so wrap it with ` and create Index on name and time column together

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent solution. I had tried it and it worked perfectly. Thank you very much. now I think I can continue my work.
0

Try it.

 Select time, name, number from [table name] group by time, name having max(number)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.