[英]how to join multiple tables in mysql (can it be handled in php)?
有一个博客用户界面,我必须显示发布该博客的用户的姓名和照片。在该部分下方,我需要显示该帖子中被标记的人员的姓名
表格是这样的:
profile_tbl
id user_id full_name pic
---------- + ------------------ + -------------------- -+ ----------------- +
1 2031 xyz img1.jpg
2 4582 abc img2.jpg
3 104 user1 img.jpg
4 3309 user2 aa.jpg
blog_tbl
id creator_id post_id message create_date
---------- + ------------------ + -------------------- -+ ----------------- + ----------------------------- ------------ +
1 2031 21 my first post 2014-01-14 19:30:17
2 4582 22 this is a test 2014-01-14 18:20:03
agged_users
id creator_id post_id tagged_user_id
---------- + ------------------ + -------------------- -+ ----------------------------------- +
1 2031 21 4582
2 2031 21 104
3 2031 21 3309
如何通过单个查询获取此信息? 要求:-1.发布此博客的用户的名称2.发布此博客的用户的图片
3.在这篇文章中被标记的用户名和ID
blog_tbl
需要包含user_id(current user id)
列,以便您可以分配作者,而且tagged_users
还需要包含user_id(current user id)
以便您知道谁标记了此人。
通过单个查询获取数据的一种方法是
select `p1`.`first_nm`,`p1`.`prf_pic`,
`b1`.`crt_date`,`b1`.`desc`,
group_concat( distinct(`p2`.`first_nm`)) as `tagged_names`,
group_concat( distinct(`p2`.`my_id`)) as `user_ids`
from `my_profile` `p1`
Inner join `conversation` `b1` on `b1`.`creator` = `p1`.`my_id`
Left JOIN `notification` `t1` on `t1`.`creator` = `p1`.`my_id`
Left join `my_profile` `p2` on `p2`.`my_id` != `p1`.`my_id`
AND `t1`.`creator` = `p1`.`my_id` AND `t1`.`to_id` = `p2`.`my_id`
group by `p1`.`my_id`
该查询将列出所有带逗号分隔的标记用户名,以及带有逗号分隔的相应用户ID。 这样您就可以根据需要操纵结果
这是我创建的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.