gpt4 book ai didi

php - 合并两个 phpmysql 查询

转载 作者:行者123 更新时间:2023-11-30 22:57:54 26 4
gpt4 key购买 nike

我想合并这两个 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, admin_id, family_id, post_by, photo_idpost_categorygeneration_idvisible_tohighlightsuper_admin_post

2) admin_user_master 这是我的第二个数据库字段。
user_id, family_id, first_name, last_name, password, email_id, 用户级别

这里是我显示墙贴数据的条件:
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";

最佳答案

使用 Join 的概念本身

select a.col1,a.col2,b.col1,b.col3 from first_table as a, second_table as b where a.col1=condition and b.col1=condition

关于php - 合并两个 phpmysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460690/

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