【问题标题】:Dsictint bi-directional relation明确的双向关系
【发布时间】:2016-07-10 10:29:52
【问题描述】:

我有一个表来描述数据库中其他表之间的关系。每个用户都可以拥有任何文件,每个文件都可以拥有任何用户。

如果我得到一个文件的关系并且我没有这个文件与用户的关系,但用户与那个文件有关系。我想看看。

当两者都与自己有关系时,我不想看到两次记录。

作为输入,我有记录类型和记录 ID。如何实现?

闭表:

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | int(11)      | NO   | PRI | NULL    | auto_increment |
| record_id          | int(11)      | NO   |     | NULL    |                |
| record_type        | varchar(200) | NO   |     | NULL    |                |
| second_record_id   | int(11)      | NO   |     | NULL    |                |
| second_record_type | varchar(200) | NO   |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+

样本数据:

+----+-----------+-------------+------------------+--------------------+
| id | record_id | record_type | second_record_id | second_record_type |
+----+-----------+-------------+------------------+--------------------+
|  1 |         1 | files       |                1 | users              |
|  2 |         2 | users       |                1 | files              |
|  3 |         3 | users       |                1 | files              |
|  4 |         2 | files       |                1 | users              |
|  5 |         1 | users       |                1 | files              |
|  6 |         1 | files       |                3 | users              |
+----+-----------+-------------+------------------+--------------------+

我试过了

SELECT * FROM closure 
WHERE record_id=1 OR second_record_id = 1 
AND record_type="files" OR second_record_type="files" 
GROUP BY "files" 
HAVING record_id=1 OR second_record_id=1

但它让我有了一个关系:

+----+-----------+-------------+------------------+--------------------+
| id | record_id | record_type | second_record_id | second_record_type |
+----+-----------+-------------+------------------+--------------------+
|  1 |         1 | files       |                1 | users              |
+----+-----------+-------------+------------------+--------------------+

我想要的结果是:

+----+-----------+-------------+------------------+--------------------+
| id | record_id | record_type | second_record_id | second_record_type |
+----+-----------+-------------+------------------+--------------------+
|  1 |         1 | files       |                1 | users              |
|  2 |         2 | users       |                1 | files              |
|  6 |         1 | files       |                3 | users              |
+----+-----------+-------------+------------------+--------------------+

Sql fiddle


编辑

我终于删除了 id 列。

【问题讨论】:

  • 我看不到想要的结果
  • 我只是把我现在研究的东西放了,可能信息太多,要不要删除“研究部分”?
  • 您应该根据提供的样本添加您想要获得的结果
  • 这个问题写的很烂,我看到的多对多和Junction Table创建两张表。
  • 我知道,但有时很难解释自己的问题。这些名字肯定很差,当我看到它们时——我不知道第一眼看到的是什么。我已经阅读了你的答案。谢谢。

标签: mysql sql junction-table


【解决方案1】:

我认为您只想针对每种记录类型比较同一类型的“1”和“文件”。这应该会产生您想要的结果:

SELECT c.*
FROM closure c
WHERE (record_id = 1 and record_type = 'files') OR
      (second_record_id = 1 and second_record_type = 'files');

【讨论】:

【解决方案2】:

您没有聚合功能,因此如果您不想 distinct add distinct 子句(但使用 id 不允许正常工作), group by 和 having 子句是无用的

SELECT  * 
FROM closure 
WHERE ( record_id=1 OR second_record_id = 1 ) 
AND  (record_type="files" OR second_record_type="files" )

SELECT distinct record_id, record_type, second_record_id, second_record_type 
FROM closure 
WHERE ( record_id=1 OR second_record_id = 1 )
AND ( record_type="files" OR second_record_type="files" ) 

【讨论】:

  • 不幸的是,它显示了所有记录。我想查看记录 id 1 和类型“文件”的连接位置。但也不要两次显示相同的关系。
  • 它显示用户 1 的文件 2,这是不希望的,它没有区分用户 3 文件 1 的关系
  • 根据您的示例,查询还应显示第 3 行 .. 尝试发布 sqlfiddle 以便我们检查 .. 查询的行为 ..
  • 我什至尝试过构建某种内部连接,但甚至无法使其工作sqlfiddle.com/#!9/665af6/13
猜你喜欢
  • 2011-02-09
  • 2016-12-05
  • 2014-07-23
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2012-03-27
相关资源
最近更新 更多