Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MySQL查询查找具有匹配行的ID_Mysql_Sql - Fatal编程技术网

使用MySQL查询查找具有匹配行的ID

使用MySQL查询查找具有匹配行的ID,mysql,sql,Mysql,Sql,上面是我的一个表格示例。 任务是为表提供一个目标item_id值,返回一个与目标行相同的item_id 在上面的示例中,提供4255的item_id将返回6544,此时这两个item_id值都位于三行中,除entry_id外,每一行都相互匹配 本质上,我需要找出数据库中是否有另一个item_id,它在所有方面都与目标相同。如果它有相同的行,但也在其他行中找到,则不会将其归类为匹配项 作为SQL查询的一部分,是否可以执行此类操作? 我目前正在C代码中执行此操作,其中我逐个遍历包含目标item_id

上面是我的一个表格示例。 任务是为表提供一个目标item_id值,返回一个与目标行相同的item_id

在上面的示例中,提供4255的item_id将返回6544,此时这两个item_id值都位于三行中,除entry_id外,每一行都相互匹配

本质上,我需要找出数据库中是否有另一个item_id,它在所有方面都与目标相同。如果它有相同的行,但也在其他行中找到,则不会将其归类为匹配项

作为SQL查询的一部分,是否可以执行此类操作?
我目前正在C代码中执行此操作,其中我逐个遍历包含目标item_id的每一行,寻找匹配项。这似乎效率很低。

我认为MySQL中最简单的方法是使用group\u concat。这是一个小技巧,但应该可以很好地工作-假设您可以稍微灵活地使用NULL:

这是怎么回事?它会为您想要的字段和所有项目生成所有统计信息。然后执行左连接以匹配其他字段中的值。然后,聚合将检查匹配统计数据的数量是否与预期数量匹配


一个优点是,此版本对空值或分隔符没有奇怪的限制。

假设您没有重复项,组合项\u id、stat\u id、stat\u type、int\u value、string\u value是唯一的,并且只有string\u value可以为空,然后,您可以在完全匹配的情况下进行联接,并比较两个项目的行数和数学数必须相等

select i.item_id
from (select distinct item_id from t) i cross join
     (select stat_id, stat_type, int_value, string_value
      from t where item_id = 4255
     ) s left join
     t
     on t.stat_id = s.stat_id and
        t.stat_type = s.stat_type and
        t.int_value is not distinct from s.int_value and
        t.string_value is not distinct from s.string_value
group by i.item_id
having count(*) = count(t.stat_id);

演示:

这不仅是一个有趣的问题,而且对于网站上的第一个问题,它写得非常好。
select t.item_id
from (select item_id,
             group_concat(stat_id, '|', stat_type, '|', int_value, '|', coalesce(string_value, '<NULL>' order by stat_id) as fields
      from t
      group by item_id
     ) t join
     (select item_id,
             group_concat(stat_id, '|', stat_type, '|', int_value, '|', coalesce(string_value, '<NULL>' order by stat_id) as fields
      from t
      where item_id = 4255
     ) tspecial
     on tspecial.fields = t.fields;
select i.item_id
from (select distinct item_id from t) i cross join
     (select stat_id, stat_type, int_value, string_value
      from t where item_id = 4255
     ) s left join
     t
     on t.stat_id = s.stat_id and
        t.stat_type = s.stat_type and
        t.int_value is not distinct from s.int_value and
        t.string_value is not distinct from s.string_value
group by i.item_id
having count(*) = count(t.stat_id);
select t2.item_id
from t t1
join t t2 using(stat_id, stat_type, int_value)
where t1.item_id = 4255
  and t2.item_id <> t1.item_id
  and t2.string_value <=> t1.string_value
group by t1.item_id, t2.item_id
having count(*) = (select count(*) from t where t.item_id = 4255)
   and count(*) = (select count(*) from t where t.item_id = t2.item_id)