简体   繁体   English

将数据库表中的分组值与关联数组进行比较

[英]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数组,它们都具有相同的两个键: pIdname The name of the arrays are added , deleted & updated . 数组的名称已addeddeletedupdated

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可以addeddeletedupdated

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 . 我要的是能够运行来检查,如果一组单查询pIdname对(其中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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM