gpt4 book ai didi

mysql - 查询中具有多个联接的重复值

转载 作者:行者123 更新时间:2023-11-29 07:25:40 26 4
gpt4 key购买 nike

我正在尝试获取对特定个人资料的评论,但是一旦超过一个与该个人资料具有不同关系 (relation_id) 的人发表评论,我就会得到重复、三次等结果。

涉及的表如下:

用户 -用户身份用户名用户姓氏用户图像路径

个人资料评论 -评论id个人资料ID用户身份评论正文评论日期

个人资料用户关系 -用户身份个人资料ID关系ID

Relation_types -关系 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

希望这会对您有所帮助。

关于mysql - 查询中具有多个联接的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com