我对mysql
中的查询有疑问。我正在Laravel中编写Web应用程序,并且需要为我的数据编写原始查询。查询是
WITH sorted_messages AS (
SELECT
IF(user_from = 3, user_to, user_from) AS other_user,
content,
created_at,
ROW_NUMBER() OVER (PARTITION BY other_user ORDER BY created_at DESC) AS rn
FROM messages AS m
WHERE user_from = 3 OR user_to = 3
)
SELECT
name,
p.image_url as image_url,
users.id,
content,
sm.created_at
FROM
users LEFT JOIN sorted_messages AS sm ON users.id = other_user
LEFT JOIN profiles AS p ON users.id = p.user_id
WHERE (rn = 1 OR rn IS NULL) AND users.id != 3
ORDER BY created_at DESC
LIMIT 10
我确保mysql
ONLY_FULL_GROUP_BY
模式已禁用。我将config/database.php
更改为在'mysql'
下有此名称:
'modes' => [
"STRICT_TRANS_TABLES",
"NO_ZERO_IN_DATE",
"NO_ZERO_DATE",
"ERROR_FOR_DIVISION_BY_ZERO",
"NO_AUTO_CREATE_USER",
"NO_ENGINE_SUBSTITUTION",
],
当我在Linux上运行它时,它可以运行,但是在Windows上却不能。回应是
Illuminate\Database\QueryException: 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 'sorted_messages AS (
我尝试在ONLY_FULL_GROUP_BY
设置中手动禁用mysql
模式,但没有帮助。