【发布时间】:2017-07-12 19:52:24
【问题描述】:
我已经尝试了 3 天来解决这个问题 - 现在我需要你们 :-)
我需要加入 3 个表:
table data table user table collection
data_id | colors user_id | user_name collection_id | user_id | data_id
---------------- ------------------- ---------------------------------
1 | blue 1 | Brian 1 | 1 | 1
2 | red 2 | Jason 2 | 2 | 3
3 | green 3 | Marie 3 | 1 | 3
4 | yellow 4 | 3 | 2
输出是用户特定的。这意味着布莱恩不想看到蓝色和绿色,杰森不想看到绿色,玛丽不想看到红色。 这应该给出以下输出:
for Brian for Jason for Marie
id | colors id | colors id | colors
----------- ----------- -----------
2 | red 1 | blue 1 | blue
4 | yellow 2 | red 3 | green
4 | yellow 4 | yellow
这是我最好的尝试(目前)如何解决这个问题:
SELECT
*
FROM
data d
INNER JOIN
user u ON u.user_id = d.user_id
LEFT JOIN
collection c ON c.data_id = d.data_id
WHERE
c.user_id <> '1' OR c.collection_id IS NULL
结果是,Brian 的选择将得到整理 - 这很好!但是因为 Jason 也不想看到绿色,所以仍然选择了 Brian。 相当混乱!
我希望我能写得或多或少可以理解 =)
非常感谢所有帮助和提示!
干杯
编辑:此解决方案已扩展并通过此解决方案变得更短:replace the id with the username from an other table in a CROSS JOIN in MySQL
【问题讨论】: