MySQL:返回另一个表中不存在相关条目的字段
首先,对标题感到抱歉,因为我不是以英语为母语的人,这很难表达。换句话说,我想要实现的是:
- 我试图从表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LEFT JOIN 返回“左”表中的所有记录,即使它们在“右”表中没有对应的记录。这些记录的正确表字段将设置为 NULL。使用 WHERE 删除所有其他内容。
我想我第一次没有正确理解你的意思。您在单个域的
aliases
中有多个条目,并且您只想显示别名表中没有以“postmaster”开头的条目的域?在这种情况下,您应该使用
NOT IN
,如下所示: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: