我有以下查询:
SELECT s0_.id, s0_.caption, s0_.lowresimageurl, s0_.medresimageurl, s0_.highresimageurl, s0_.price
FROM app_instagram_shop_picture s0_
LEFT JOIN app_instagram_shop s1_ force index (idx_locked_deletedAt)
LEFT JOIN app_instagram_picture_category s2_
LEFT JOIN app_instagram_first_level_category s3_
ON s0_.shop_id = s1_.id
AND s1_.deletedAt IS NULL
AND s0_.id = s2_.picture_id
AND s2_.first_level_category_id = s3_.id
WHERE (s0_.isShown = 1
AND s1_.id != 32179
AND s1_.expirydate IS NULL
AND s1_.deletedAt IS NULL
AND s1_.isLocked = 0
AND s1_.owner_id IS NULL
AND s0_.id != 2598561
AND s0_.deletedAt IS NULL
AND s0_.isStyleInspiration = 0
AND s0_.isLocked = 0
AND s3_.id = 11
)
AND (s0_.deletedAt IS NULL)
ORDER BY s0_.updated DESC
LIMIT 16
由于某种原因,这导致我出现以下错误:
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 'WHERE (s0_.isShown = 1
AND s1_.id != 32179
AND s1_.expirydate IS N' at line 10
然而,我似乎无法弄清问题是什么..
答案 0 :(得分:0)
请记住,每个SELECT语句只有一个WHERE子句,并且在检查开始时使用它,之后只使用AND(我已经多次忘记了这个并且稍后放置了额外的WHERE子句在链中)
答案 1 :(得分:0)
SELECT s0_.id, s0_.caption, s0_.lowresimageurl, s0_.medresimageurl, s0_.highresimageurl, s0_.price
FROM app_instagram_shop_picture s0_
LEFT JOIN app_instagram_shop s1_ force index (idx_locked_deletedAt)
LEFT JOIN app_instagram_picture_category s2_
LEFT JOIN app_instagram_first_level_category s3_
ON s0_.shop_id = s1_.id
AND s1_.deletedAt IS NULL
AND s0_.id = s2_.picture_id
AND s2_.first_level_category_id = s3_.id
WHERE s0_.isShown = 1
AND s1_.id != 32179
AND s1_.expirydate IS NULL
AND s1_.deletedAt IS NULL
AND s1_.isLocked = 0
AND s1_.owner_id IS NULL
AND s0_.id != 2598561
AND s0_.deletedAt IS NULL
AND s0_.isStyleInspiration = 0
AND s0_.isLocked = 0
AND s3_.id = 11
AND s0_.deletedAt IS NULL
ORDER BY s0_.updated DESC
LIMIT 16
答案 2 :(得分:0)
使用JOIN,您需要为每个加入的表指定ON
或USING
以指示加入关系。
您有3个LEFT JOINs
但只有一个ON
条件。您可能打算这样做:
FROM app_instagram_shop_picture s0_
LEFT JOIN app_instagram_shop s1_ force index (idx_locked_deletedAt)
ON s0_.shop_id = s1_.id
AND s1_.deletedAt IS NULL
LEFT JOIN app_instagram_picture_category s2_
ON s0_.id = s2_.picture_id
LEFT JOIN app_instagram_first_level_category s3_
ON s2_.first_level_category_id = s3_.id
(在第一个ON s0_.shop_id = s1_.id
中指定其他谓词将不适用于其他连接表)
答案 3 :(得分:0)
支架我的原因错误请删除支架并使用它
SELECT s0_.id, s0_.caption, s0_.lowresimageurl, s0_.medresimageurl, s0_.highresimageurl, s0_.price
FROM app_instagram_shop_picture s0_
LEFT JOIN app_instagram_shop s1_ force index (idx_locked_deletedAt)
LEFT JOIN app_instagram_picture_category s2_
LEFT JOIN app_instagram_first_level_category s3_
ON s0_.shop_id = s1_.id
AND s1_.deletedAt IS NULL
AND s0_.id = s2_.picture_id
AND s2_.first_level_category_id = s3_.id
WHERE s0_.isShown = 1
AND s1_.id != 32179
AND s1_.expirydate IS NULL
AND s1_.deletedAt IS NULL
AND s1_.isLocked = 0
AND s1_.owner_id IS NULL
AND s0_.id != 2598561
AND s0_.deletedAt IS NULL
AND s0_.isStyleInspiration = 0
AND s0_.isLocked = 0
AND s3_.id = 11
AND (s0_.deletedAt IS NULL)
ORDER BY s0_.updated DESC
LIMIT 16