MySQL:返回另一个表中不存在相关条目的字段

发布于 2024-10-12 02:36:31 字数 2308 浏览 3 评论 0原文

首先,对标题感到抱歉,因为我不是以英语为母语的人,这很难表达。换句话说,我想要实现的是:

  • 我试图从表 virtual_domains 中获取所有域名,其中 virtual_aliases 表中没有以“postmaster@%”开头的相应条目。

因此,如果我有两个域:

foo.org
example.org

它们的别名如下:

[email protected]       => [email protected]
[email protected] => [email protected]
[email protected]   => [email protected]

我希望查询仅返回域“foo.org”,因为“example.org”缺少 postmaster 别名。

这是表布局:

mysql> show columns from virtual_aliases;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment | 
| domain_id   | int(11)      | NO   | MUL | NULL    |                | 
| source      | varchar(100) | NO   |     | NULL    |                | 
| destination | varchar(100) | NO   |     | NULL    |                | 
+-------------+--------------+------+-----+---------+----------------+

mysql> show columns from virtual_domains;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
| name  | varchar(50) | NO   |     | NULL    |                | 
+-------+-------------+------+-----+---------+----------------+

我尝试了多个小时的 IF、CASE、LIKE 查询,但没有成功。我不需要最终的解决方案,也许只是一个带有一些解释的提示。谢谢!

First, sorry for the title, as I'm no native english-speaker, this is pretty hard to phrase. In other words, what I'm trying to achieve is this:

  • I'm trying to fetch all domain names from the table virtual_domains where there is no corresponding entry in the virtual_aliases table starting like "postmaster@%".

So if I have two domains:

foo.org
example.org

An they got aliases like:

[email protected]       => [email protected]
[email protected] => [email protected]
[email protected]   => [email protected]

I want the query to return only the domain "foo.org" as "example.org" is missing the postmaster alias.

This is the table layout:

mysql> show columns from virtual_aliases;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment | 
| domain_id   | int(11)      | NO   | MUL | NULL    |                | 
| source      | varchar(100) | NO   |     | NULL    |                | 
| destination | varchar(100) | NO   |     | NULL    |                | 
+-------------+--------------+------+-----+---------+----------------+

mysql> show columns from virtual_domains;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
| name  | varchar(50) | NO   |     | NULL    |                | 
+-------+-------------+------+-----+---------+----------------+

I tried for many hours with IF, CASE, LIKE queries with no success. I don't need a final solution, maybe just a hint with some explanation. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪我们做朋友吧 2024-10-19 02:36:31
SELECT * FROM virtual_domains AS domains
LEFT JOIN  virtual_aliases AS aliases
ON domains.id = aliases.domain_id
WHERE aliases.domain_id IS NULL

LEFT JOIN 返回“左”表中的所有记录,即使它们在“右”表中没有对应的记录。这些记录的正确表字段将设置为 NULL。使用 WHERE 删除所有其他内容。


我想我第一次没有正确理解你的意思。您在单个域的 aliases 中有多个条目,并且您只想显示别名表中没有以“postmaster”开头的条目的域?

在这种情况下,您应该使用 NOT IN ,如下所示:

SELECT * FROM virtual_domains AS domains
WHERE domains.id NOT IN (
    SELECT domain_id
    FROM virtual_aliases
    WHERE whatever_column LIKE "postmaster@%"
)
SELECT * FROM virtual_domains AS domains
LEFT JOIN  virtual_aliases AS aliases
ON domains.id = aliases.domain_id
WHERE aliases.domain_id IS NULL

LEFT JOIN returns all records from the "left" table, even they have no corresponding records in "right" table. Those records will have the right table fields set to NULL. Use WHERE to strip all the others.


I guess I didn't understand you correctly the first time. You have several entries in aliases for single domain, and you want to display only those domains that don't have an entry in aliases table that starts with "postmaster"?

In this case you are should use NOT IN like this:

SELECT * FROM virtual_domains AS domains
WHERE domains.id NOT IN (
    SELECT domain_id
    FROM virtual_aliases
    WHERE whatever_column LIKE "postmaster@%"
)
笑着哭最痛 2024-10-19 02:36:31
select id,domain from virtual_domains 
where id not in (select domain_id from virtual_aliases)
select id,domain from virtual_domains 
where id not in (select domain_id from virtual_aliases)
面犯桃花 2024-10-19 02:36:31
SELECT * FROM virtual_domains vd
LEFT JOIN virtual_aliases va ON vd.id = va.domain_id
AND va.destination NOT LIKE 'postmaster@%';
SELECT * FROM virtual_domains vd
LEFT JOIN virtual_aliases va ON vd.id = va.domain_id
AND va.destination NOT LIKE 'postmaster@%';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文