php MySQL中的减号运算符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8386280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:35:16  来源:igfitidea点击:

MINUS operator in MySQL?

phpmysql

提问by AssamGuy

I have some tables where I am getting the emails. And I don't want to get the emails in table tbl_unsubscribe. I wrote the query like :

我有一些表格可以用来接收电子邮件。而且我不想收到 table 中的电子邮件tbl_unsubscribe。我写了这样的查询:

SELECT cand_email FROM tbl_cand_data
UNION
SELECT emp_email FROM tbl_emp_data
UNION
SELECT email FROM tbl_uptade_list
UNION
SELECT feed_email FROM tbl_feedback
UNION
SELECT admin_email FROM tbl_admin_emails    

But I am getting a syntax error. Is the MINUSoperator not valid for MySQL ?

但我收到语法错误。是MINUS运营商无效为MySQL?

回答by Michael Berkowski

A NOT IN()subquery can be used here, since MySQL doesn't support MINUS.

一个NOT IN()子查询可以在这里使用,因为MySQL不支持MINUS

SELECT 
  cand_email
FROM tbl_cand_data 
WHERE can_email NOT IN (SELECT un_email FROM tbl_unsubscribe)

It can also be done with a LEFT JOIN, looking for NULLs in the un_emailcolumn:

也可以使用 a 来完成,LEFT JOINun_email列中查找 NULL :

SELECT cand_email 
FROM
   tbl_cand_data
   LEFT JOIN tbl_unsubscribe ON tbl_cand_data.can_email = tbl_unsubscribe.un_email
WHERE tbl_unsubscribe.un_email IS NULL

To exclude them from a bunch of UNIONoperations, wrap the UNIONgroup in ()as a subquery:

要将它们从一系列UNION操作中排除,请将UNION组包装()为子查询:

SELECT email FROM (
  SELECT cand_email AS email FROM tbl_cand_data
  UNION
  SELECT emp_email AS email FROM tbl_emp_data
  UNION
  SELECT email FROM AS email tbl_uptade_list
  UNION
  SELECT feed_email AS email FROM tbl_feedback
  UNION
  SELECT admin_email AS email FROM tbl_admin_emails
) email_list
WHERE email NOT IN (SELECT un_email FROM tbl_unsubscribe)

回答by Maxim Krizhanovsky

Unfortunately MINUSand INTERSECTare not supported by MySQL, but you can get the same result using JOINfor MINUS, UNIONfor INTERSECT.

遗憾的是MINUSINTERSECT用的MySQL不支持,但你可以使用得到相同的结果JOINMINUSUNIONINTERSECT

SELECT cand_email FROM tbl_cand_data
LEFT JOIN tbl_unsubscribe ON (cand_email = un_email)
WHERE un_email IS NULL