2011-07-07 87 views
1
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 

它说:SQL中的语法错误?哪里?

[错误] 1064 - 你在你的 SQL语法错误;检查手册, 对应于您的MySQL服务器 版本的正确语法使用 'WHERE bio_community_events'附近。 group_id = '1'

我找不到语法错误!

编辑:

我包都变成支架,并添加 “在哪儿”。不工作...仍然是同样的错误。

新建查询:

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:

我看着你的榜样。愚蠢的我!谢谢。

+2

线索在问题中,实际上是问题的标题! ;-) –

+0

给你编辑:你只需要一个'WHERE',就像我的答案中的例子。 – Jacob

回答

5

您有2 WHERE条款,您需要用ANDOR替换您的第二个WHERE

编辑:像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子句。

SELECT syntax

Expression syntax

如果你想连接2倍的表达,你必须使用ORAND等 你不需要再写WHERE条款。这一切都进入一个WHERE

+0

+1。在这两个子查询 –

+0

做到了。我甚至把所有的where子句放在括号中。对彼此而言。不工作...看我的编辑。 – daGrevis