使用PHP从mysql表中获取多行并描述时间界限
我有一个如下所示的mysql表使用PHP从mysql表中获取多行并描述时间界限,php,mysql,Php,Mysql,我有一个如下所示的mysql表 ip_add mac time 10.0.0.97 00 14 2A 2F 72 FE 2013-09-18 16:35:47 10.0.0.97 00 14 2A 2F 72 FE 2013-09-19 08:48:02 10.0.0.98 08 CC 68 7
ip_add mac time
10.0.0.97 00 14 2A 2F 72 FE 2013-09-18 16:35:47
10.0.0.97 00 14 2A 2F 72 FE 2013-09-19 08:48:02
10.0.0.98 08 CC 68 71 A1 C0 2013-09-18 16:35:47
10.0.0.98 08 CC 68 71 A1 C0 2013-09-19 08:48:02
我有一个php脚本,我想打印以下几行
The ip address 10.0.0.97 was used by MAC 00 14 2A 2F 72 FE from 2013-09-18 16:35:47 to 2013-09-19 08:48:02
The ip address 10.0.0.97 was used by MAC 00 14 2A 2F 72 FE from 2013-09-19 08:48:02 to present(i.e there has not been a further entry of that ip )
The ip address 10.0.0.98 was used by MAC 00 14 2A 2F 72 FE from 2013-09-18 16:35:47 to 2013-09-19 08:48:02
The ip address 10.0.0.97 was used by MAC 00 14 2A 2F 72 FE from 2013-09-19 08:48:02 to present
我已经写了我的脚本如下
$var = $_POST['IP'](ip which will be obtained from form);
mysql_connect('localhost','root','mysql');
mysql_select_db('logs');
$result=mysql_query("select ip_add,mac from arp_table where ip_add='$var'");
$row=mysql_fetch_row($result);
$count=mysql_query("select COUNT (*) from arp_table where ip_add='$var'");
$row2 =mysql_fetch_row($count);
echo "The number of instances this ip address was used is {$row2[0]}<br/>"
if($row2==1){
$time=mysql_query("select ip_add, time from arp_table where ip_add='$var'")
$time2=mysql_fetch_row($time);
echo "It was used from {$time2[1]} and it is still in use<br/>";}
else
{$time3=mysql_query("select ip_add,time from arp_table where ip_add='$var'");
while($row3=mysql_fetch_assoc($time3)){
echo "This ip was used by MAC {$row[1]} from (*I`M STUCK HERE*) to (*I`M STUCK HERE*)"}
}
如何让它获得由同一ip注册的上一个时间并将其与下一个在线时间关联
SELECT
ip_add,
mac,
MIN(time) as from_time,
MAX(time) as to_time
FROM
my_table
GROUP BY
ip_add,
mac
或者,如果要为每一行执行以下操作:
SELECT
a.ip_add,
a.mac,
a.time as from_time,
(
SELECT
time
FROM
my_table b
WHERE
b.ip_add = a.ip_add
AND a.time < b.time
ORDER BY
b.time
LIMIT 1
) as to_time
FROM
my_table a
试试这个:
SELECT
ip_add,
mac,
MIN(time) as from_time,
MAX(time) as to_time
FROM
my_table
GROUP BY
ip_add,
mac
或者,如果要为每一行执行以下操作:
SELECT
a.ip_add,
a.mac,
a.time as from_time,
(
SELECT
time
FROM
my_table b
WHERE
b.ip_add = a.ip_add
AND a.time < b.time
ORDER BY
b.time
LIMIT 1
) as to_time
FROM
my_table a
这部分我必须参考上一个ip的下一个相同ip时间,并提及它,因为该ip不时被使用。我得到了你的观点,我发布了一个答案。请检查这部分,我必须参考上一个ip的下一个相同ip时间,并提及它,因为该ip不时被使用。我得到了你的观点,我发布了一个答案,请检查它在这种情况下,我不想找到最短的时间,但我只需要以前的工作time@sosytee为此,您需要使用第二个查询您是否知道在php脚本中放置该查询的位置,因为我现在放置的位置没有返回任何结果,所以您不需要其他查询,只需要这一个查询,然后迭代结果并输出结果。如果我不想找到最短时间,但只需要前一个查询,这是否有效time@sosytee为此,你需要使用第二个问题你知道去哪里吗将它放在php脚本中,因为我现在放置它的地方没有返回任何结果。您不需要其他查询,只需要这一个,然后迭代结果并输出它们