MySQL有多个WHERE子句

时间:2013-05-06 12:09:21

标签: mysql sql

我有一个包含此信息的数据库(示例)

EMAIL                 COMING    
info@domain.com       google
test@example.net      google
text@example.net      yahoo
info@super.net        google
testing@foo.com       yahoo
testing@foo.com       google

我只提取来自“google”的值,而不是“yahoo”。所以,在我的SQL中,我只会阅读

info@domain.com

info@super.net

因为另一个是来自google&&雅虎。

所以,在代码中,像

这样的东西
SELECT * FROM mytable WHERE COMING = 'google' AND COMING != 'yahoo'

但显然我的代码错了。

你可以帮帮我吗?谢谢:))

2 个答案:

答案 0 :(得分:2)

FROM是保留关键字,不应在表列名中使用它。

如果您想保留此关键字,可以使用此请求:

SELECT * 
FROM mytable 
WHERE `FROM` = 'google'
AND email NOT IN (
    SELECT email 
    FROM mytable
    WHERE `FROM`='yahoo'
)

修改了以下评论

答案 1 :(得分:2)

了解非规范化表如何生成性能低下的查询以满足您的需求,

SELECT a.*
FROM   TableName a
       INNER JOIN
      (
          SELECT substr
          FROM
          (
              SELECT email,
                     REVERSE(SUBSTRING(REVERSE(SUBSTRING(email, LOCATE('@', email) + 1)),LOCATE('.', REVERSE(SUBSTRING(email, LOCATE('@', email) + 1)))+1)) substr,
                     coming
              FROM   tableName
          ) s
          GROUP BY substr
          HAVING COUNT(DISTINCT coming) = 1 AND
                 MAX(coming) IN ('yahoo','google')
      ) b ON a.email LIKE CONCAT('%@',b.substr,'.%')

输出

╔═════════════════╦════════╗
║      EMAIL      ║ COMING ║
╠═════════════════╬════════╣
║ info@domain.com ║ google ║
║ info@super.net  ║ google ║
╚═════════════════╩════════╝