mysql连接表创建缺少日期

时间:2011-01-17 17:31:07

标签: sql mysql join

不确定如何做到这一点。我整个早上都和JOINS搞砸了。我所能得到的只是日期范围,但日期只是在应该为空白的日期重复。<​​/ p>

这是一个基本查询

SELECT `time`,`protocal`,(`octet`/1024) as `octet10243`,`percent`,`div`,FROM_UNIXTIME(`time`, '%Y-%m-%d') as `newtime3` FROM ipport
WHERE FROM_UNIXTIME(`time`, '%Y-%m-%d') >= '2011-01-05' AND FROM_UNIXTIME(`time`, '%Y-%m-%d') <= '2011-01-08' AND (`protocal` = 'Echo' ) AND `div` = 'XDIV'

结果就是这样。

"time";"protocal";"octet10243";"percent";"div";"newtime3"
"1290923100";"Echo";"92844.07421875";"1.04435";"XDIV";"2011-01-06"
"1291009500";"Echo";"95110.106445312";"1.0796";"XDIV";"2011-01-07"

我还有另一张表格,其中包含2011-01-01至2011-01-17的日期。我正在用它加入。

这是我正在寻找的结果。

"time";"protocal";"octet10243";"percent";"div";"newtime3"
"0";"Echo";"0";"0";"XDIV";"2011-01-05"
"1290923100";"Echo";"92844.07421875";"1.04435";"XDIV";"2011-01-06"
"1291009500";"Echo";"95110.106445312";"1.0796";"XDIV";"2011-01-07"
"0";"Echo";"0";"0";"XDIV";"2011-11-08"

这是我对JOINING的囤积尝试......

SELECT makeupdate.date, FROM_UNIXTIME(ipport.time, '%Y-%m-%d'),`time`,`protocal`,(`octet`/1024) as `octet10243`,`percent`,`div`,FROM_UNIXTIME(`time`, '%Y-%m-%d')
as `newtime3` FROM ipport  JOIN makeupdate ON FROM_UNIXTIME(ipport.time, '%Y-%m-%d') >= '2011-01-05'
AND FROM_UNIXTIME(ipport.time, '%Y-%m-%d') <= '2011-01-08' AND (`protocal` = 'Echo' ) AND `div` = 'XDIV'

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

从您的日期表开始,并将LEFT JOIN连接到您的ipport表,以便从日期表中获取所有行以及ipport表中的匹配行。

SELECT m.date, FROM_UNIXTIME(i.time, '%Y-%m-%d'),`time`,`protocal`,(`octet`/1024) as `octet10243`,`percent`,`div`,FROM_UNIXTIME(`time`, '%Y-%m-%d') as `newtime3` 
    FROM makeupdate m
        LEFT JOIN ipport i
            on m.date = FROM_UNIXTIME(ipport.time, '%Y-%m-%d')
                AND (`protocal` = 'Echo' ) 
                AND `div` = 'XDIV'
    WHERE m.date >= '2011-01-05' AND m.date <= '2011-01-08'