Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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 单表记录_Mysql_Sql_Self Join - Fatal编程技术网

Mysql 单表记录

Mysql 单表记录,mysql,sql,self-join,Mysql,Sql,Self Join,很抱歉,我不能把它说得更清楚。。。 假设DB表有3列: 对象\u id-某些实体 关系键-对象的某些属性 bundle\u id-我们必须用这个id概括不同的对象 表具有唯一的[对象id,关系键]: 单个对象不能有重复的关系\u键, 但是不同的对象可以具有相同的关系\u键 对图片的一些理解: 很多对象可以通过relation_键建立深层关系,所有这些对象都将与bundle_id 如何仅使用一个查询就用正确的值更新bundle_id列? 我会写程序,但这种方法不适合我 我寻找这样的语句:

很抱歉,我不能把它说得更清楚。。。 假设DB表有3列:

  • 对象\u id-某些实体
  • 关系键-对象的某些属性
  • bundle\u id-我们必须用这个id概括不同的对象
表具有唯一的[对象id关系键]: 单个对象不能有重复的关系\u键, 但是不同的对象可以具有相同的关系\u键

对图片的一些理解:

很多对象可以通过relation_键建立深层关系,所有这些对象都将与bundle_id

如何仅使用一个查询就用正确的值更新bundle_id列? 我会写程序,但这种方法不适合我


我寻找这样的语句:
“更新示例[在…上加入示例]设置bundle\u id=…其中…”

mysql有“之前”模式:

CREATE TABLE `example` (
  `bundle_id` INT(11) DEFAULT NULL,
  `object_id` INT(11) NOT NULL,
  `relation_key` INT(11) NOT NULL,
  PRIMARY KEY (`object_id`,`relation_key`) 
);

INSERT INTO `example`(`object_id`, `relation_key`) 
VALUES (1, 4), (1, 5), (1, 6), (2, 6), (2, 7), (2, 8), (3, 4), 
(3, 9), (3, 10), (4, 11), (4, 12), (4, 13), (5, 14), (5, 15), (5, 16), (6, 17), (6, 11), (6, 18);

以下是“before”示例(SQLFIDLE此时卡住)

如果您执行查询,“after”将类似于:

UPDATE `example` SET `bundle_id` = 1 WHERE `object_id` IN (1, 2, 3);
UPDATE `example` SET `bundle_id` = 2 WHERE `object_id` IN (4, 6);
UPDATE `example` SET `bundle_id` = 3 WHERE `object_id` IN (5);

object1 related to object2 by key=6, 
object3 related TO object1 by key=4, 
so ... objs 1, 2, 3 are related together. 
here must be first bundle_id=1. 
there is no other keys linking another objects to 1, 2, 3

object_id=4 related to object_id=6 by key=11
so ... obj [4, 6] are related together. 
here must be second bundle_id=2, 
there is no other keys linking another objects to 4, 6  

object_id=5 has no relations to other objects
all object's key belong to itself. 
here must be second bundle_id=3, 
there is no other keys linking another objects to 5   

请解释“使用正确的值更新bundle_id列”应该使用什么逻辑。什么值是正确的?什么值不正确?编辑问题并添加一些有代表性的样本数据,以及最终结果应该是什么样子。两个表都应该作为文本而不是图像发布。另外,将标签添加到您使用的DBMS及其版本中。是的,现在的问题好多了,但我不知道如何在MySQL中实现。据我所知,它不支持递归查询。请解释应该使用什么逻辑来“使用正确的值更新bundle\u id列”。什么值是正确的?什么值不正确?编辑问题并添加一些有代表性的样本数据,以及最终结果应该是什么样子。两个表都应该作为文本而不是图像发布。另外,将标签添加到您使用的DBMS及其版本中。是的,现在的问题好多了,但我不知道如何在MySQL中实现。据我所知,它不支持递归查询。