我刚刚在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
答案 0 :(得分: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
或移动你在哪里的条件
JOIN blocks B
ON (B.user_id = M.user_id AND B.blocked_id = B.user_id)
答案 1 :(得分: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})";
然后执行SELECT
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})";