4

我拥有的是 mysql 数据库中的两个表。

一个表包含具有值的字段"example@example.com"

另一张桌子没有...

我想做的是检查两个表中是否有一个值匹配的字段"example@example.com"

这是我的查询不起作用:

(#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 '' at line 13)

SELECT CASE WHEN
EXISTS (
    SELECT address, suburb, city, postCode, province
    FROM user_postal
    INNER JOIN user_info ON user_postal.id = user_info.id
    WHERE user_info.emailContact = 'example@example.com'
)
OR EXISTS (
    SELECT address, suburb, city, postCode, province
    FROM user_postal
    INNER JOIN user_business_info ON user_postal.id = user_business_info.id
    WHERE user_business_info.emailContact = 'example@example.com'
)

我想返回与address, suburb, city, postCode, province用户 id 和 email 匹配的字段的值example@example.com。(这可能在两个表中的任何一个中,但肯定在一个或另一个中)

谁能告诉我我哪里出了问题?任何这方面的信息将不胜感激。谢谢!

4

3 回答 3

4

这个怎么样:

SELECT address, suburb, city, postCode, province
FROM user_postal
INNER JOIN user_info ON user_postal.id = user_info.id
WHERE user_info.emailContact = 'example@example.com'

UNION

SELECT address, suburb, city, postCode, province
FROM user_postal
INNER JOIN user_business_info ON user_postal.id = user_business_info.id
WHERE user_business_info.emailContact = 'example@example.com'
于 2012-06-20T19:03:58.160 回答
1
SELECT address, suburb, city, postCode, province 
FROM user_postal
        INNER JOIN user_business_info ON (user_postal.id = user_business_info.id AND user_info.emailContact = 'example@example.com')
                                        OR (user_postal.id = user_info.id AND user_business_info.emailContact = 'example@example.com')
于 2012-06-20T19:05:04.443 回答
1

这是您要使用UNION.

于 2012-06-20T19:03:26.860 回答