提问人:G Kal 提问时间:6/24/2020 最后编辑:G Kal 更新时间:6/25/2020 访问量:62
MySQL和PHP选择不同的行
MySQL and PHP select different row
问:
我有这张表
配置
ID Location Position CheckData UpdateDT
1 House1 Floor1 20 2020-06-24 18:12:20
2 House1 Floor1 30 2020-06-24 13:54:16
3 House1 Floor2 45 2020-06-24 10:06:34
4 House2 Roof1 70 2020-05-12 13:27:43
5 House1 Floor1 35 2020-05-12 12:20:12
我喜欢从配置表中获取每个“home”和“position”的最新更新值,例如select
1 House1 Floor1 20 2020-06-24 18:12:20
3 House1 Floor2 45 2020-06-24 10:06:34
4 House2 Roof1 70 2020-05-12 13:27:43
我试过这个,但它不起作用,因为我不知道如何添加位置
select * from Configuration where Location = 'house 1' order by Configuration.UpdateDT desc limit 1
这是我目前拥有的PHP代码
$sqlsyntax = "SELECT * FROM `DevicesLocations`";
$resultfromsql = mysqli_query($connectionstring, $sqlsyntax);
while( $row = mysqli_fetch_array($resultfromsql) )
{
echo $row['Location'] . " " . $row['Comments'];
echo "<br />";
$sql2 = "SELECT * from Configuration where location='";
$sql2 = $sql2.$row['Location']."' order by UpdateDT desc limit 1";
echo $sql2."<br>";
$result2 = mysqli_query($connectionstring, $sql2);
while( $row2 = mysqli_fetch_array($result2) )
{
echo "..........".$row2['SensorPosition'] . " " . $row2['CheckData'] ;
echo "<br />";
}
echo "------------------------------<br>";
}
输出
House1 first home
...... floor1 20
House2 second home
...... Roof1 70
期望输出
House1 first home
...... floor1 20
...... floor2 45
House2 second home
...... Roof1 70
如果我使用以下内容,我如何包含最新日期?select
SELECT * FROM `Configuration` group BY Configuration.SensorPosition
答:
0赞
G Kal
6/25/2020
#1
我想我已经找到了答案。所以,如果我错了,请纠正我
SELECT m.* from Configuration m
left join Configuration b on
m.SensorPosition = b.SensorPosition and
m.UpdateDT < b.UpdateDT where b.UpdateDT is null
评论
WHERE
select * from Configuration WHERE Location = "House1" order by Configuration.UpdateDT desc limit 1