【发布时间】:2025-11-28 15:50:01
【问题描述】:
我想结合这两个 php mysql 查询并希望得到一个结果集数组。
第一个查询给出所有成员和 postby 相关的帖子,第二个查询给出所有与管理员相关的帖子,member_id 为 0,post_by 为 0,admin_id 不是 0,super_admin_post 0。
数据库:
1) wall_post_master 这是我的第一个数据库字段。wp_id, wp_cat_id, wl_text, wp_img, datetime, displevel, mg_id, member_id, family_id, @987654334 @、post_by、photo_id、post_category、generation_id、visible_to、highlight、super_admin_post
2) admin_user_master 这是我的第二个数据库字段。user_id, family_id, first_name, last_name, password, email_id, user_level
这里显示墙贴数据的条件:
1)如果 mg_id 退出时有多个 member_id 与逗号相关。
2)如果 visible_to 退出时有多个 member_id 与逗号相关。
3)如果 member_id 以当前 member_id 退出。
4)如果 member_id 在 post_by 中退出。
5)如果 member_id 为 0 且 post_by 为 0 且 super_admin_post = 0 则我想显示为管理员帖子。
第一个查询:
$grpsearch = "";
foreach ($arrseltran as $grpid) {
$grpsearch .= " OR ( FIND_IN_SET('" . $grpid . "', wp.mg_id) AND displevel = 3)";
}
if($time == ""){ $time .= " WHERE"; }else{ $time .=" AND"; }
$time .= "
wp.family_id = " . $_SESSION['logft']['family_id'] . " AND
wp.post_category = 0 AND
(
wp.member_id = " . $_SESSION['logft']['member_id'] . " OR
wp.post_by = " . $_SESSION['logft']['member_id'] . " OR
FIND_IN_SET('" . $_SESSION['logft']['member_id'] . "', wp.visible_to) " . $grpsearch . " OR
displevel = 1
)";
$timeline = "SELECT wp.*,
wp.member_id,
wp.datetime AS TimeSpent,
wp_cat.font_color,
photo.photo_path,
mm.first_name,
mm.middle_name,
mm.last_name,
mm1.first_name AS to_first_name,
mm1.middle_name AS to_middle_name,
mm1.last_name AS to_last_name
FROM wall_post_master wp
INNER JOIN
wallpost_cat_master wp_cat ON wp_cat.wp_cat_id = wp.wp_cat_id
INNER JOIN
member_master mm ON mm.member_id = (CASE WHEN (wp.post_by = 0) THEN wp.member_id ELSE wp.post_by END)
INNER JOIN
member_master mm1 ON mm1.member_id = wp.member_id
LEFT JOIN
photo_master photo ON photo.photo_id = mm.photo_id
" . $time ." ORDER BY Timespent DESC";
第二次查询:
$timeline1 = "SELECT wpa.*,
wpa.family_id,
wpa.datetime AS TimeSpent,
wp_cat.font_color,
wpa.wp_img,
c.family_id,
c.family_name,
c.editor_photo,
aum.first_name,
aum.last_name
FROM wall_post_master wpa
INNER JOIN
wallpost_cat_master wp_cat ON wp_cat.wp_cat_id = wpa.wp_cat_id
INNER JOIN
config c ON c.family_id = wpa.family_id
INNER JOIN
admin_user_master aum ON aum.family_id = wpa.family_id
where
aum.user_level = 1 and
wpa.admin_id <> 0 and
wpa.family_id='".$_SESSION['logft']['family_id']."' and
wpa.member_id=0 and
wpa.post_by=0 and
wpa.super_admin_post=0
ORDER
BY Timespent DESC";
【问题讨论】:
-
UNION是您要找的吗? -
我尝试过联合但根本不工作,因为第一个查询依赖于 member_id 或 post_by viseversa(CASE WHEN (wp.post_by = 0) THEN wp.member_id ELSE wp.post_by END),第二个查询 if member_id和 post_by 都是 0,然后它的家庭管理员帖子显示给所有成员,所以我想将这两个查询加入一个带有递减时间戳的结果集中。
-
您能否展示一些示例数据,以及您想要作为组合查询的结果得到什么?
-
这是我展示墙贴数据的 5 个条件。