duai1683 2014-09-30 15:54
浏览 254
已采纳

mysql选择2个表并按日期排序1个(更接近当前时间)

This is my problem.

table ns_leagues:

id   name
1    League 1
2    League 2
3    League 3

table ns_upcoming:

upID   league   date
1      1        1410390000
2      2        1411990200
3      3        1412010000

I have this 2 columns and I want to select the leagues from table ns_leagues and order by the row from the ns_upcoming with the date more close to the current time.

I tried several ways but nothings is working so far.

FAIL:

SELECT id 
FROM ns_leagues 
WHERE id in (
    SELECT league 
    FROM ns_upcoming 
    WHERE date<='".$now."' 
    ORDER BY date ASC

SELECT DISTINCT id 
from ns_leagues a 
LEFT JOIN ns_upcoming v ON  a.id = v.league AND v.date<= '".$now."' 
ORDER BY v.date ASC

CURRENT:

SELECT * FROM ".PREFIX."leagues ORDER BY id DESC

This is what I have right now (on the right side: PROXIMOS PARTIDOS): http://www.nuno-silva.pt/jobs/mark/index.php

"En vivo" means that date >= time(); and it's not finished. The times are countdowns for the match.

I need to order the leagues by date (selecting for that the row with the closest date to time(); from column date of table ns_upcoming of the selected league)

Could you give me a help on this one please?

  • 写回答

1条回答 默认 最新

  • duanhong4274 2014-09-30 16:00
    关注

    You'd need to sort by the time DIFFERENCE between "now" and the match's time, e.g.

    SELECT ...
    ...
    ORDER BY DATEDIFF(ns_upcoming.date, curdate())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗