结合两个phpmysql查询

时间:2014-08-23 09:54:46

标签: php mysql sql mysqli

我想结合这两个php mysql查询,并希望获得一个结果集数组。 第一个查询提供所有成员和帖子相关帖子,第二个查询提供所有与管理员相关的帖子member_id为0,post_by为0,admin_id不为0且super_admin_post 0。

数据库:
1)wall_post_master这是我的第一个数据库字段 wp_idwp_cat_idwl_textwp_imgdatetimedisplevelmg_idmember_id,{{ 1}},admin_idfamily_idpost_byphoto_idpost_categorygeneration_idvisible_to,{{1} }

2)admin_user_master这是我的第二个数据库字段 highlightsuper_admin_postuser_idfamily_idfirst_namelast_namepassword

这里我的条件显示wallpost数据:
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则我想显示为admin post。

第一次查询:

email_id

第二次查询:

user_level

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你可以使用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);
?>

答案 2 :(得分:0)

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

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

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