-2

我刚刚在 Stackoverflow 上得到了一些帮助并得到了正确的查询。但是他不知道任何 PHP,所以现在我也需要帮助。

这是查询和PHP:

if($do_resync) {
    $sql = "
        with MY_VIEW as
        (
        SELECT
            room_user.*,
            user.username AS user_name,
            user.group_id AS group_id, USER.USER_ID
        FROM {$x7->dbprefix}room_users room_user
        INNER JOIN {$x7->dbprefix}users user ON
            user.id = room_user.user_id
        WHERE
            room_id IN ({$rooms})
        )
        SELECT COUNT(id) AS block FROM
            blocks AS B, MY_VIEW AS M
        WHERE
            B.user_id = M.user_id
        AND B.blocked_id = B.user_id
    ";
    $st = $db->prepare($sql);
    $st->execute(); (THIS IS LINE 230 that the error points at)
    $users = $st->fetchAll();
    $output['users'] = $users;
}

echo json_encode($output);

这是我得到的错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MY_VIEW as ( SELECT room_user.*, user.username AS user_name, u' at line 6' in pages/sync.php:230 Stack trace: #0 pages/sync.php(230): PDOStatement->execute() #1 index.php(50): require('/home/vol5_3/by...') #2 {main} thrown in pages/sync.php on line 230

编辑:

SELECT COUNT(id) AS block FROM
    blocks AS B, 
(
    SELECT
        room_user.*,
        user.username AS user_name,
        user.group_id AS group_id, USER.USER_ID
    FROM {$x7->dbprefix}room_users room_user
    INNER JOIN {$x7->dbprefix}users user ON
        user.id = room_user.user_id
    WHERE
        room_id IN ({$rooms})
) AS M
WHERE
    B.user_id = M.user_id
AND B.blocked_id = B.user_id
4

2 回答 2

0

Mysql 不支持WITH子句你怎么能像下面那样做,从你的查询中加入你的数据集

SELECT * FROM 
        (
        SELECT
            room_user.*,
            user.username AS user_name,
            user.group_id AS group_id, USER.USER_ID
        FROM {$x7->dbprefix}room_users room_user
        INNER JOIN {$x7->dbprefix}users user ON
            user.id = room_user.user_id
        WHERE
            room_id IN ({$rooms})
        ) AS M
JOIN (SELECT * FROM blocks ) AS B
ON (B.user_id = M.user_id)
WHERE B.blocked_id = B.user_id

编辑

SELECT * FROM 
            (
            SELECT
                room_user.*,
                user.username AS user_name,
                user.group_id AS group_id, USER.USER_ID
            FROM {$x7->dbprefix}room_users room_user
            INNER JOIN {$x7->dbprefix}users user ON
                user.id = room_user.user_id
            WHERE
                room_id IN ({$rooms})
            ) AS M
    JOIN blocks  B
    ON (B.user_id = M.user_id)
    WHERE B.blocked_id = B.user_id

或在 on 部分移动您的 where 条件

    JOIN blocks  B
    ON (B.user_id = M.user_id AND B.blocked_id = B.user_id)
于 2013-11-13T18:55:02.980 回答
0

我可以想到两种方法来解决您的问题:

1 创建视图:

    $sql="CREATE VIEW MY_VIEW as
    SELECT
        room_user.*,
        user.username AS user_name,
        user.group_id AS group_id, USER.USER_ID
    FROM {$x7->dbprefix}room_users room_user
    INNER JOIN {$x7->dbprefix}users user ON
        user.id = room_user.user_id
    WHERE
        room_id IN ({$rooms})";

然后做你的选择

2 创建一个临时表:(http://dev.mysql.com/doc/refman/5.1/en/create-table.html

    $sql1 = "CREATE TEMPORARY TABLE tmp_room_users_x0001 (
     -- the list of fields here ...
    )";


    // populate the table ...
    $sql2 = "INSERT INTO tmp_room_users_x0001 (SELECT
        room_user.*,
        user.username AS user_name,
        user.group_id AS group_id, USER.USER_ID
    FROM {$x7->dbprefix}room_users room_user
    INNER JOIN {$x7->dbprefix}users user ON
        user.id = room_user.user_id
    WHERE
        room_id IN ({$rooms})";
于 2013-11-13T19:01:53.767 回答