我正在尝试修改我的查询以仅输出我的sql表(ipaccounting)中的数据,列(timeanddate)在另一个表(datapackages),列(thresholdtimespandays)中指定的最后“天数”内
ipaccounting,timeanddate
中存储的数据看起来像“2017-10-16 07:45:53”而datapackages,thresholdtimespandays
只是一个像“7”这样的实数(从现在开始的7天) )
我需要在查询中添加/更改哪些内容?
我目前的查询是:
SELECT ip_address
,SUM(upload_bytes) as upload_bytes
,SUM(download_bytes) as download_bytes
,sum(upload_bytes + download_bytes) as totalbytes
,package_id
,username
,networkaccess
,packagename
,speedlimit
,threshold
,throttlelimit
,datalimitamount
,datalimitrange
,thresholdtimespandays
FROM (
(SELECT ipaccounting.src_address as ip_address
,SUM(ipaccounting.bytes) AS upload_bytes
,0 as download_bytes
,users.username
,users.networkaccess
,datapackages.package_id
,datapackages.packagename
,datapackages.speedlimit
,datapackages.threshold
,datapackages.throttlelimit
,datapackages.datalimitamount
,datapackages.datalimitrange
, datapackages.thresholdtimespandays
FROM ipaccounting
join users on users.ipaddress = ipaccounting.src_address
join datapackages on datapackages.package_id = users.datapackage
WHERE dst_address NOT BETWEEN INET_NTOA('192.168.0.1') AND INET_NTOA('192.168.255.254')
GROUP BY src_address)
UNION ALL
(SELECT ipaccounting.dst_address as ip_address
,0 AS upload_bytes
,SUM(ipaccounting.bytes) as download_bytes
,users.username
,users.networkaccess
,datapackages.package_id
,datapackages.packagename
,datapackages.speedlimit
,datapackages.threshold
,datapackages.throttlelimit
,datapackages.datalimitamount
,datapackages.datalimitrange
,datapackages.thresholdtimespandays
FROM ipaccounting
join users on users.ipaddress = ipaccounting.dst_address
join datapackages on datapackages.package_id = users.datapackage
WHERE src_address NOT BETWEEN INET_NTOA('192.168.0.1') AND INET_NTOA('192.168.255.254')
GROUP BY dst_address)
) a
GROUP BY ip_address
ORDER BY INET_ATON(ip_address)
我不确定如何使用我的数据库datapackages,thresholdtimespandays
中指定的天数来取当前时间和减去该值,然后提供数据。
谢谢:)
答案 0 :(得分:0)
我已经玩过并自己想出来了!
我的更新查询是:
SELECT ip_address
,SUM(upload_bytes) as upload_bytes
,SUM(download_bytes) as download_bytes
,sum(upload_bytes + download_bytes) as totalbytes
,package_id
,username
,networkaccess
,packagename
,speedlimit
,threshold
,throttlelimit
,datalimitamount
,accountingdays
FROM (
(SELECT ipaccounting.src_address as ip_address
,SUM(ipaccounting.bytes) AS upload_bytes
,0 as download_bytes
,users.username
,users.networkaccess
,datapackages.package_id
,datapackages.packagename
,datapackages.speedlimit
,datapackages.threshold
,datapackages.throttlelimit
,datapackages.datalimitamount
,datapackages.accountingdays
FROM ipaccounting
join users on users.ipaddress = ipaccounting.src_address
join datapackages on datapackages.package_id = users.datapackage
WHERE dst_address NOT BETWEEN INET_NTOA('192.168.0.1') AND
INET_NTOA('192.168.255.254')
and timeanddate BETWEEN SUBDATE(CURRENT_TIMESTAMP(), INTERVAL
datapackages.accountingdays DAY) AND CURRENT_TIMESTAMP()
GROUP BY src_address)
UNION ALL
(SELECT ipaccounting.dst_address as ip_address
,0 AS upload_bytes
,SUM(ipaccounting.bytes) as download_bytes
,users.username
,users.networkaccess
,datapackages.package_id
,datapackages.packagename
,datapackages.speedlimit
,datapackages.threshold
,datapackages.throttlelimit
,datapackages.datalimitamount
,datapackages.accountingdays
FROM ipaccounting
join users on users.ipaddress = ipaccounting.dst_address
join datapackages on datapackages.package_id = users.datapackage
WHERE src_address NOT BETWEEN INET_NTOA('192.168.0.1') AND
INET_NTOA('192.168.255.254')
and timeanddate BETWEEN SUBDATE(CURRENT_TIMESTAMP(), INTERVAL
datapackages.accountingdays DAY) AND CURRENT_TIMESTAMP()
GROUP BY dst_address)
) a
GROUP BY ip_address
ORDER BY INET_ATON(ip_address)
我也对我的数据库进行了一些更改,例如datapackage.thresholdtimespandays
到datapackage.accountingdays
,因此我的查询与我的问题略有不同。