【问题标题】:combine two phpmysql queries结合两个 phpmysql 查询
【发布时间】: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_byphoto_idpost_categorygeneration_idvisible_tohighlightsuper_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 个条件。

标签: php mysql sql mysqli


【解决方案1】:

使用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

【讨论】:

    【解决方案2】:

    你可以使用mysqli函数:

    mysqli_multi_query()
    

    mysqli::multi_query()
    

    http://php.net/manual/en/mysqli.multi-query.php

    来自链接的完整示例:

    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $query  = "SELECT CURRENT_USER();";
    $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
    
    /* execute multi query */
    if ($mysqli->multi_query($query)) {
        do {
            /* store first result set */
            if ($result = $mysqli->store_result()) {
                while ($row = $result->fetch_row()) {
                    printf("%s\n", $row[0]);
                }
                $result->free();
            }
            /* print divider */
            if ($mysqli->more_results()) {
                printf("-----------------\n");
            }
        } while ($mysqli->next_result());
    }
    
    /* close connection */
    $mysqli->close();
    ?>
    Procedural style
    
    <?php
    $link = mysqli_connect("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $query  = "SELECT CURRENT_USER();";
    $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
    
    /* execute multi query */
    if (mysqli_multi_query($link, $query)) {
        do {
            /* store first result set */
            if ($result = mysqli_store_result($link)) {
                while ($row = mysqli_fetch_row($result)) {
                    printf("%s\n", $row[0]);
                }
                mysqli_free_result($result);
            }
            /* print divider */
            if (mysqli_more_results($link)) {
                printf("-----------------\n");
            }
        } while (mysqli_next_result($link));
    }
    
    /* close connection */
    mysqli_close($link);
    ?>
    

    【讨论】:

    • 多查询比只做两个查询有什么好处?
    • 你节省了连接和认证时间,(与查询处理时间相比,这个时间很长)
    • 身份验证仅在您调用new mysqli 时发生,因此无关紧要。
    • 无论如何,我认为他真正在寻找的是如何使用UNION 将它们组合成一个查询。
    • @Barmar 不,每次向数据库发送查询时都会进行身份验证
    【解决方案3】:

    如果您匹配两个查询中所选字段的名称,您也可以使用 UNION

    http://dev.mysql.com/doc/refman/5.0/en/union.html

    http://www.mysqltutorial.org/sql-union-mysql.aspx

    【讨论】: