如何检查所有 mysql 结果是否相等

How to check if all mysql results are equal

我现在遇到了困难 - 我正在一个房地产网站上工作,卖家有一些开发项目,每个开发项目中都有单独的单元(例如,有 4 间公寓的房子)。

开发项目在数据库中有一个 status 列,指定它是否 AvailableReservedSold 而每个单元在 table.

中也有相同的列

我要解决的问题是,即使所有单元都标记为 已售出,开发项目仍然显示为 可用 -当然,卖家可以自行更改,但很多时候不会。

我想制作一个脚本(最终将用作 cron 作业)来检查所有开发单元的状态,如果所有单元都已设置,则将开发的状态更改为 Reserved 保留,例如。

我编写了以下代码来回显所有属性及其各自的单位,使用 foreach 开发中的单位 foreach

<?php
$get_developments_sql = "SELECT property_id, property_name, status FROM properties WHERE is_newdevelopment = 1 AND is_active = 1";
$get_developments = mysqli_query($dbconnect, $get_developments_sql);

foreach($get_developments as $development):
    $get_units_sql = "SELECT id, property_id, property_name, status FROM property_units WHERE property_id =".$development['property_id'];
    $get_units = mysqli_query($dbconnect, $get_units_sql);
    ?>
    <span>Development ID: <?=$development['property_id'];?></span><br>
    <span>Development Status: <?=$development['status'];?></span><br>
    <span>Development Name: <?=$development['property_name'];?></span><br><br>
    <!-- Each Unit -->
    <?php
    foreach($get_units as $unit):
        ?>
        <span>Unit Name: <?=$unit['property_name'];?> -> <?=$unit['status'];?></span><br>
        <?php
    endforeach;
    ?>
    <hr>
    <?php
endforeach;
?>

问题是我需要检查来自单个开发的 所有 单元状态是否相同,因为如果它们是相同的,我想更改状态发展到各单位都有的

所以在这种情况下,我希望能够通过 PHP 查看所有单位是否具有 status 通过使用 [=30] 具有相同的值=]$unit['status'] 值什么的。

有人知道我该怎么做吗?我只需要知道我可以使用什么方法来验证所有单位结果在 status 列中是否具有相同的值。

它不需要在 PHP 如果有更好的方式通过查询或其他东西。

您可以使用这样的 SQL 查询来识别其所有子项都只有一种状态的属性:

select properties.property_id, max(property_units.status) as max_status
from properties
join property_units on properties.property_id = property_units.property_id
group by properties.property_id
having count(distinct property_units.status) = 1

确定上述查询产生预期结果后,将其转换为 update-with-join 查询:

update properties
join (
    select property_id, max(status) as max_status
    from property_units
    group by property_id
    having count(distinct status) = 1
) as pu_status on properties.property_id = pu_status.property_id
set properties.status = pu_status.max_status

DB<>Fiddle

非常感谢 @Salman A 对查询外观的帮助,

我后来设法用它来将开发单元的状态应用到开发本身的状态,

感谢大家的宝贵意见,

    <?php

    // This query allows to check the results where developments have all units with the same status
    $get_developments_sql = "SELECT properties.property_id, properties.status, max(property_units.status) as the_status
                            FROM properties
                            JOIN property_units on properties.property_id = property_units.property_id
                            GROUP BY properties.property_id
                            HAVING count(distinct property_units.status) = 1";
    $get_developments = mysqli_query($dbconnect, $get_developments_sql);

foreach($get_developments as $development):?>
    
        <?php

            // Only for Visual Representation - Query to fetch all units from the development we're iterating
            $get_units_sql = "SELECT id, property_id, property_name, status FROM property_units WHERE property_id =".$development['property_id'];
            $get_units = mysqli_query($dbconnect, $get_units_sql);
            
            // Update the developments we're currently receiving from the first query and update their status according to the consensus status of their units
            // Using prepared statements
            $update_development_status_sql = "UPDATE properties SET status = ? WHERE property_id = ?";
            $stmt = $dbconnect->prepare($update_development_status_sql);

            // Update the property status with the unanimous status from its units through the property_id they're linked to (the development itself)
            $stmt->bind_param('si', $development['the_status'], $development['property_id']);
            $stmt->execute();
        
        ?>
        
        <!-- Visual representation so I can see in a list the developments and their respective units and the statuses of each so it's easier to see the change in the developments statuses -->
        <span>Development ID: <?=$development['property_id'];?></span><br>
        <span>Development Status: <strong><?=$development['status'];?></strong></span><br>
        <span>Status of All Units: <strong><?=$development['the_status'];?></strong></span><br><br>
        
        <!-- Each Unit -->
        <?php foreach($get_units as $unit):?>
            <span>Unit Name: <?=$unit['property_name'];?> -> <?=$unit['status'];?></span><br>
        <?php endforeach;?>
        <hr>
        
    <?php endforeach;?>

干杯