PDO在哪里,并且foreach避免重复

时间:2019-01-17 10:15:53

标签: php mysql pdo

我有两个表“ 订阅者”和“ 状态”。我喜欢向订阅者发送状态消息,但仅当他们订阅该服务时。

表“ 订户”包含:

id | mail | service (comma separated)
-----------------------------------------------
1 | name1@gmail.com | TV, Internet 
2 | name2@gmail.com | Cloud, Internet 
3 | name3@gmail.com | Phone, Cloud

表“ 状态”包含:

id | title | message | service (comma separated)
------------------------------------------------------
1 | Disruption | TV services offline | TV
2 | Maintenace | All services offline | TV, Internet, Cloud 
3 | Disruption | Some services offline | TV, Internet 

我的代码:

$stmt = $pdo->prepare("SELECT * FROM status WHERE id = '2'"); // send message 2
$stmt->execute();
$row = $stmt->fetch();

$array = explode(", ", $row->service);

foreach ($array as $value) {

    $stmt = $pdo->prepare("SELECT * FROM subscribers WHERE service = ? OR service LIKE ? OR service LIKE ? OR service LIKE ?");
    $stmt->execute(["$value","$value,%","%$value, %","%, $value"]);
}

我的问题:

状态消息具有多个服务时,订户会多次收到同一条消息。我该如何避免呢?还是有更好的方法来做到这一点?

0 个答案:

没有答案