Dsictint双向关系

时间:2016-07-10 10:29:53

标签: mysql sql junction-table

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

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

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

作为输入,我有记录类型和记录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列。

2 个答案:

答案 0 :(得分:1)

你没有聚合功能所以如果你使用不同的添加不同的条款(但是使用id不允许正确的工作),则group by和having子句是无用的。

accepted:false

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

答案 1 :(得分:1)

我想你只想比较" 1"和#34;文件"对于每种记录类型,在相同类型。这应该产生你想要的结果:

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