简体   繁体   中英

Syntax Error in SQL?? 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'
 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`.`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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM