SELECT `bio_community_events`.`id`,
`bio_community_events`.`begin_on`,
`bio_community_events`.`name`
FROM `bio_community_events`
JOIN `bio_contacts`
ON (`bio_contacts`.`contact_id` = `bio_community_events`.`user_id`)
JOIN `bio_community_groups`
ON (`bio_community_groups`.`id` = `bio_community_events`.`group_id`)
WHERE `bio_contacts`.`user_id` = '33'
WHERE `bio_community_events`.`group_id` = '1'
LIMIT 10
UNION ALL
SELECT `bio_community_events`.`id`,
`bio_community_events`.`begin_on`,
`bio_community_events`.`name`
FROM `bio_community_events`
JOIN `bio_contacts`
ON (`bio_contacts`.`user_id` = `bio_community_events`.`user_id`)
JOIN `bio_community_groups`
ON (`bio_community_groups`.`id` = `bio_community_events`.`group_id`)
WHERE `bio_contacts`.`contact_id` = '33'
WHERE `bio_community_events`.`group_id` = '1'
LIMIT 10
It says:
[Err] 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
bio_community_events
.group_id
= '1'
I can't find the syntax mistake!
Edit:
I wrapped all into brackets and add "AND WHERE". Not working... still the same error.
New query:
SELECT `bio_community_events`.`id`,
`bio_community_events`.`begin_on`,
`bio_community_events`.`name`
FROM `bio_community_events`
JOIN `bio_contacts`
ON (`bio_contacts`.`contact_id` = `bio_community_events`.`user_id`)
JOIN `bio_community_groups`
ON (`bio_community_groups`.`id` = `bio_community_events`.`group_id`)
WHERE (`bio_contacts`.`us开发者_如何学运维er_id` = '33')
AND WHERE (`bio_community_events`.`group_id` = '1')
LIMIT 10
UNION ALL
SELECT `bio_community_events`.`id`,
`bio_community_events`.`begin_on`,
`bio_community_events`.`name`
FROM `bio_community_events`
JOIN `bio_contacts`
ON (`bio_contacts`.`user_id` = `bio_community_events`.`user_id`)
JOIN `bio_community_groups`
ON (`bio_community_groups`.`id` = `bio_community_events`.`group_id`)
WHERE (`bio_contacts`.`contact_id` = '33')
AND WHERE (`bio_community_events`.`group_id` = '1')
LIMIT 10
Edit #2:
I looked at your example. Stupid me! Thanks.
You have 2 WHERE
clauses, you need to replace your second WHERE
with an AND
or OR
.
Edit: like ypercube pointed out, you have the error in both subqueries of the UNION
-clause.
For example:
SELECT `bio_community_events`.`id`,
`bio_community_events`.`begin_on`,
`bio_community_events`.`name`
FROM `bio_community_events`
JOIN `bio_contacts`
ON (`bio_contacts`.`user_id` = `bio_community_events`.`user_id`)
JOIN `bio_community_groups`
ON (`bio_community_groups`.`id` = `bio_community_events`.`group_id`)
WHERE `bio_contacts`.`contact_id` = '33'
AND `bio_community_events`.`group_id` = '1'
LIMIT 10
Edit 2:
A WHERE
clause takes a boolean expression. You can only have one WHERE
clause per query.
SELECT syntax
Expression syntax
If you want to connect 2 expressions, you have to use OR
or AND
, etc.
You don't need to write another WHERE
clause. It all goes into one WHERE
.
精彩评论