[英]Comparing grouped values in a database table with an associative array
I have three PHP arrays which all have the same two keys: pId
and name
. 我有三个PHP数组,它们都具有相同的两个键:
pId
和name
。 The name of the arrays are added
, deleted
& updated
. 数组的名称已
added
, deleted
和updated
。
The table I want to compare with looks like this: 我要与之比较的表如下所示:
id
relationId
pId
name
changeType
The rows are grouped by the relationId and there may be multiple pId & name pairs per relationId. 这些行按relationshipId分组,每个relationshipId可能有多个pId和名称对。
changeType
can be either added
, deleted
or updated
. changeType
可以added
, deleted
或updated
。
What I want is to be able to run a single query that checks if a group of pId
& name
pairs (where the changeType
matches the array name) that matches the three arrays exactly, exists in the table and return the relationId
. 我要的是能够运行来检查,如果一组单查询
pId
和name
对(其中changeType
,这三个阵列完全匹配的数组名称相匹配),存在于表中并返回relationId
。
Is this possible? 这可能吗?
Example 例
Table: 表:
+--------+----------------+---------+---------------+----------------+
| **id** | **relationId** | **pId** | **name** | **changeType** |
+--------+----------------+---------+---------------+----------------+
| 1 | 1 | 1 | Smith | added |
+--------+----------------+---------+---------------+----------------+
| 2 | 1 | 2 | John | updated |
+--------+----------------+---------+---------------+----------------+
| 3 | 1 | 3 | Dexter | deleted |
+--------+----------------+---------+---------------+----------------+
| 4 | 1 | 4 | Heisenberg | added |
+--------+----------------+---------+---------------+----------------+
| 5 | 2 | 4 | Heisenberg | added |
+--------+----------------+---------+---------------+----------------+
| 6 | 2 | 3 | Dexter | updated |
+--------+----------------+---------+---------------+----------------+
| 7 | 2 | 3 | Dexter Morgan | updated |
+--------+----------------+---------+---------------+----------------+
PHP Array: PHP阵列:
$added = array(
[1] = array(
pId => 4,
name => 'Heisenberg'
)
)
$deleted = array(
//Empty
)
$updated = array(
[1] = array(
pId => 3,
name => 'Dexter'
)
[2] = array(
pId => 3,
name => 'Dexter Morgan'
)
)
When querying using this array the returned relationId should be 2. 使用此数组查询时,返回的relationshipId应该为2。
I solved this by comparing an imploded string from the arrays with GROUP_CONCAT
on the table. 我通过比较数组中的内嵌字符串与表上的
GROUP_CONCAT
来解决此问题。 Since you can't normally use GROUP_CONCAT
in the WHERE
clause I had to do a workaround with the FROM
clause. 由于您通常无法在
WHERE
子句中使用GROUP_CONCAT
,因此我不得不对FROM
子句进行解决。
<?php
$s = '';
foreach($updated as $u){
$s .= $u['pId'] . $u['name'] . 'updated,';
}
foreach($deleted as $d){
$s .= $d['pId'] . $d['name'] . 'deleted,';
}
foreach($added as $a){
$s .= $a['pId'] . $a['name'] . 'added,';
}
//remove last comma
$s = rtrim($s, ',');
$stmt = $mysqli -> prepare("
SELECT
relationId
FROM (
SELECT
relationId,
GROUP_CONCAT(pId, name, changeType ORDER BY changeType DESC, id ASC) AS personConcat
FROM
persons
GROUP BY relationId
) x
WHERE personConcat = ?
");
$stmt -> bind_param('s', $s);
$stmt -> execute();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.