Dsictint 双向关系

Dsictint bi-directional relation

我有 table 描述了数据库中其他 table 之间的关系。每个用户可以有任何文件,每个文件可以有任何用户。

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

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

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

关闭table:

+--------------------+--------------+------+-----+---------+----------------+
| 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 列。

你没有聚合函数,所以如果你不想 distinct add distinct 子句,则 group by 和 having 子句是无用的(但使用 id 不允许正常工作)

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" ) 

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

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