我刚刚在 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