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
它说:
[Err] 1064 - 你的错误 SQL语法;检查手册 对应于您的MySQL服务器 用于正确语法的版本 靠近'WHERE
bio_community_events
。group_id
= '1'
我找不到语法错误!
修改
我将所有内容包装在括号中并添加“AND WHERE”。不工作......仍然是同样的错误。
新查询:
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')
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
编辑#2:
我看了你的例子。愚蠢的我!感谢。
答案 0 :(得分:5)
您有2个WHERE
条款,您需要将第二个WHERE
替换为AND
或OR
。
编辑:就像ypercube指出的那样,UNION
- 子句的两个子查询都有错误。
例如:
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
编辑2:
WHERE
子句采用布尔表达式。每个查询只能有一个WHERE
子句。
如果要连接2个表达式,则必须使用OR
或AND
等。
您不需要编写另一个WHERE
子句。这一切都进入WHERE
。