Mysql 查询中具有多个联接的重复值
我试图获取对某个特定配置文件的评论,但一旦有多个与该配置文件有不同关系(relationship_id)的人发表评论,我就会得到重复、重复等结果 涉及的表格如下:Mysql 查询中具有多个联接的重复值,mysql,join,Mysql,Join,我试图获取对某个特定配置文件的评论,但一旦有多个与该配置文件有不同关系(relationship_id)的人发表评论,我就会得到重复、重复等结果 涉及的表格如下: SELECT profile_comments.*, relation_types.relation_name, users.user_first_name, users.user_image_path FROM profile_comments LEF
SELECT profile_comments.*,
relation_types.relation_name,
users.user_first_name,
users.user_image_path
FROM profile_comments
LEFT JOIN profile_user_relation ON profile_comments.profile_id = profile_user_relation.profile_id
LEFT JOIN relation_types ON relation_types.relation_id = profile_user_relation.relation_id
LEFT JOIN users ON profile_comments.user_id = users.user_id
WHERE profile_comments.profile_id = :profileId
用户-
用户id
用户名
用户名
用户图像路径
个人资料评论-
注释\u id
个人资料
用户id
评论(附体)
评论日期
配置文件与用户关系-
用户id
个人资料
关系id
关系类型-
关系id
关系名称
我的质询如下:
SELECT profile_comments.*,
relation_types.relation_name,
users.user_first_name,
users.user_image_path
FROM profile_comments
LEFT JOIN profile_user_relation ON profile_comments.profile_id = profile_user_relation.profile_id
LEFT JOIN relation_types ON relation_types.relation_id = profile_user_relation.relation_id
LEFT JOIN users ON profile_comments.user_id = users.user_id
WHERE profile_comments.profile_id = :profileId
谢谢大家! 没有测试出来,但我认为它应该有效,或者至少让你更接近它
SELECT
C.*,
RT.relation_name,
U.user_first_name,
U.user_image_path
FROM profile_comments AS C
JOIN users AS U USING(user_id)
JOIN profile_user_relation AS UR USING(user_id, profile_id)
JOIN relation_types AS RT USING(relation_id)
WHERE C.profile_id = :profileId
希望这会有点帮助 这是完美的,作品的开箱即用-谢谢!我将不得不研究使用的用法:)