我有三个 PHP 数组,它们都有相同的两个键:pId
和name
. 数组的名称是added
, deleted
& updated
。
我要比较的表如下所示:
id
relationId
pId
name
changeType
这些行按关系 ID 分组,每个关系 ID 可能有多个 pId 和名称对。changeType
可以是added
,deleted
或updated
.
我想要的是能够运行单个查询来检查与三个数组完全匹配的一组pId
&name
对(其中changeType
匹配数组名称)是否存在于表中并返回relationId
.
这可能吗?
例子
桌子:
+--------+----------------+---------+---------------+----------------+
| **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 数组:
$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'
)
)
使用此数组查询时,返回的 relationId 应为 2。