Mysql选择MAX val等条件

时间:2016-11-18 04:04:26

标签: mysql sql

我在MySql中有以下表格:

id|time|depth

id和time是主键。当最大时间的深度不为0时,我需要一个查询来选择一行。 理想情况下,我甚至需要在最大值之前的行。

让我举个例子。表格可能是这样的:

id       time      depth      
----------------------------
1        0          0   
1        10         1   
1        20         2    
2        0          0     
2        10         1

在这种情况下,它应该返回:

id       time      depth      
----------------------------  
1        20         2       
2        10         1

非常感谢

2 个答案:

答案 0 :(得分:1)

使用子查询的连接是一种方法。

POST /api/2/users/login/ HTTP/1.1
Host: 192.168.52.166:6000
User-Agent: curl/7.47.0
Accept: */*
content-type: application/json
Content-Length: 57

{"email":"emjimadhu@email.com", "password":"qwerty123"}

HTTP/1.1 201 CREATED
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 13 Nov 2016 22:00:22 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
Allow: POST, OPTIONS
Set-Cookie: csrftoken=CSRF_TOKEN; expires=Sun, 12-Nov-2017 22:00:22 GMT; Max-Age=31449600; Path=/
Set-Cookie: sessionid=6ob5doyoup7roob1o6o0burotayn4yt2; expires=Sun, 27-Nov-2016 22:00:22 GMT; httponly; Max-Age=1209600; Path=/

21b
 {"auth_token":"my auth token", "id":"5f7g5a6r-6q9b-4544-91ce-f79e57d7e2ca","email":"emjimadhu@email.com"}

答案 1 :(得分:0)

试试这个:

>>> entrylist = [1, 2, 3, 4]
>>> history = [1,]
>>> for e in entrylist:
...     for d in history[:]:
...         if d == e:
...             break;
...         else:
...             history.insert(0, e)
...             break;
...
>>> entrylist
[1, 2, 3, 4]
>>> history
[4, 3, 2, 1]