【问题标题】:Nested if statement in PHP MySQL SELECTPHP MySQL SELECT中的嵌套if语句
【发布时间】:2023-04-03 01:37:01
【问题描述】:

我需要交叉引用行。如果该行不存在,则插入它。

以下是在将其插入数据库之前需要通过的条件:

首先查找属于用户 (user_id) 的约会。

然后查找与约会 ID (appointment_id) 匹配的约会。如果约会 ID 不存在,请继续下一步。

如果约会 ID 不存在,则搜索约会是否匹配约会日期和时间 (appointment_date) (appointment_time)。

如果不存在,则将INSERT 放入数据库。

到目前为止,这是我的代码。如何使 SELECT 的嵌套 if 语句更快、更简单?

// Search for appointment by appointment ID to see if it already exists
$stmt = $dbh->prepare("SELECT id FROM users WHERE user_id = :user_id AND appointment_id = :appointment_id LIMIT 1");
$stmt->bindParam(':user_id', $userId);
$stmt->bindParam(':appointment_id', $appointmentId);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

// If appointment does not already exist, search for appointment by date and time
if(!$result) {
    $stmt = $dbh->prepare("SELECT id FROM users WHERE user_id = :user_id AND appointment_date = :appointment_date AND appointment_time = :appointment_time LIMIT 1");
    $stmt->bindParam(':user_id', $userId);
    $stmt->bindParam(':appointment_date', $appointmentDate);
    $stmt->bindParam(':appointment_time', $appointmentTime);
    $stmt->execute();
    $result2 = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!$result2) {
        // If appointment does not already exist, insert into database:
        $stmt = $dbh->prepare("INSERT INTO...")
    }
}

我怎样才能让它更快、更简单/更短?

【问题讨论】:

  • 如果尚未创建约会 ID,您如何获得它?
  • @Devon 约会 ID 来自另一个日历。我正在尝试同步两个日历
  • 考虑使用 IODKU

标签: php mysql performance select pdo


【解决方案1】:

我认为您可以尝试使用 UNION 来获得只有一个查询和一半的代码。

// Search for appointment by appointment ID to see if it already exists
$stmt = $dbh->prepare("(SELECT id FROM users WHERE user_id = :user_id AND appointment_id = :appointment_id) UNION (SELECT id FROM users WHERE user_id = :user_id AND appointment_date = :appointment_date AND appointment_time = :appointment_time) LIMIT 1");
$stmt->bindParam(':user_id', $userId);
$stmt->bindParam(':appointment_id', $appointmentId);
$stmt->bindParam(':appointment_date', $appointmentDate);
$stmt->bindParam(':appointment_time', $appointmentTime);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

// If appointment does not already exist, search for appointment by date and time
if(!$result) {
    $stmt = $dbh->prepare("INSERT INTO...")
}

我还没有真正测试过上面的这段代码。它可能需要一些调整。

如果出了什么问题,请告诉我。

【讨论】:

    【解决方案2】:

    如果您不需要区分这两个查询,只需结合您的条件:

    SELECT id FROM users WHERE user_id = :user_id AND 
     (appointment_id = :appointment_id OR 
      appointment_date = :appointment_date AND appointment_time = :appointment_time)
    LIMIT 1
    

    【讨论】:

    • 我确实需要先看约会ID,然后再看约会日期和时间
    • 好的,那么我真的不知道你的问题是什么。
    • 我只是在交叉引用我的日历和另一个日历。我正在尝试将两个日历同步在一起。在我的代码中,我试图在我的日历上搜索相同的约会/事件,从约会 ID 开始,如果没有找到,则按日期和时间。如果从中找不到任何内容,则将插入该事件,因为 db 中没有现有约会
    • 对,但是如果您需要分两步完成,那么您已经有了代码……那您还需要什么?
    • 我只是想知道是否有更快的方法来处理所有这些,特别是如果有 100000 多个约会
    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2016-07-20
    • 2010-12-24
    • 2017-05-17
    • 1970-01-01
    • 2010-09-12
    相关资源
    最近更新 更多