0

使用下面的 2 个表格,我想显示一个名称列表,下一个可用时间显示在他们的名字旁边。每个名称只有 1 个结果。你能帮我吗?谢谢鲍比

user table
|  user_ID  |  name   
|  1        |   Smith  
|  2        |   Jones 

times_available
|    ID    |   user_ID  | time_avail |
|        1 |   2        |   01:30    |
|        2 |   2        |   02:30    |
|        3 |   3        |   02:30    |
|        4 |   3        |   03:30    |
|        5 |   3        |   04:30    |

显示每个名称的下一个可用时间

|  user_ID  |  time   |
|    Smith  |   02:30 |
|    Jones  |   02:30 |

@Clodoaldo 有一个很好的答案,但我还需要显示没有即将到来的 time_avail 的名称

4

2 回答 2

2
select u.name, min(t.time_avail)
from
    user_table u
    inner join
    times_available t on u.user_id = t.user_id
where t.time_avail > current_time
group by u.user_id
于 2013-02-27T18:25:14.667 回答
0

你可以这样做

SELECT 
    u.user_ID,
    u.name,
    ta.NewTime
FROM user AS u
LEFT JOIN (
       SELECT r.* , lta.time_avail AS `NewTime` FROM times_available as r
       LEFT JOIN (SELECT * FROM times_available LIMIT 1,69596585953484) AS lta
          ON lta.ID = (SELECT MIN(ID) FROM times_available WHERE ID > r.ID GROUP BY r.ID limit 1)
) AS ta ON ta.user_ID = u.user_ID 
WHERE ta.NewTime IS NOT NULL
GROUP BY u.user_ID 

小提琴演示

于 2013-02-27T18:28:34.833 回答