简体   繁体   English

PHP-SQL-内部联接不起作用

[英]PHP - SQL- inner join not working

I have 3 tables in the database 我的数据库中有3个表

first-table-name : savedemail 第一个表名:savedemail

ID    email
================
1     user.com
2     xy.com
3     user2.com

second-table-name : volley 次表名称:凌空

mac        email
===================
12345     hhhhh.com
22222     user2.com
33333     ggggg.com

third-table-name : macadd 第三表名:macadd

 mac
=========
00000
00000
22222

what I am trying to do is compare each row of email of savedemail table to the each row of email of volley table . 我想做的是比较saveemail表的每一行电子邮件与volley表的每一行电子邮件。 if the email matches then I want to store mac of volley to mac of macadd table if it already not exists. 如果电子邮件匹配,那么我想将凌空的mac存储到macadd表的mac(如果尚不存在)。

the my code below is not inserting anything to table also no errors\\warnings 我下面的代码没有向表插入任何内容,也没有错误\\警告

Here is query I am using 这是我正在使用的查询

SELECT savedemail.email , volley.mac , volley.email
FROM savedemail
INNER JOIN volley
ON savedemail.email=volley.email
WHERE savedemail.email=volley.email REPLACE macadd (mac)
VALUES ('volley.mac')

Try this query instead: 请尝试以下查询:

INSERT INTO macadd (mac)
SELECT volley.mac 
FROM volley 
WHERE volley.email = (
    SELECT email 
    FROM savedemail 
    WHERE email = volley.email
    AND (
        SELECT count(mac) FROM macadd WHERE macadd.mac = volley.mac
    ) = 0
)

A little difficult to put into words, I'm sure you get the gist of it, but mostly you would select all the emails from savedemail's where the email value from that table matches one from the volley table, and so long as that mac doesn't exist in the macadd table, append that mac to the macadd table. 说起来有点困难,我相信您已经明白了要点,但是大多数情况下,您会从saveedmail中选择所有电子邮件,其中该表中的电子邮件值与凌空表中的一个匹配,并且只要mac不匹配即可。在macadd表中不存在,请将该mac附加到macadd表中。

Disclaimer: My MySQL knowledge is somewhat limited, so I can almost certainly assure you there are quicker/less-resource-intensive ways to do this; 免责声明:我的MySQL知识是有限的,因此我几乎可以肯定地向您保证,有更快/更少资源的方式来做到这一点。 however, the above method does work in my testing. 但是,以上方法在我的测试中确实有效。

Here's one option using the outer join / null check approach: 这是使用outer join / null检查方法的一个选项:

insert into macadd
select v.mac
from savedemail s 
    join volley v on s.email = v.email
    left join macadd m on v.mac = m.mac
where m.mac is null

Alternatively you can use not exists , but from my experience with mysql queries, the outer join / null check will generally outperform it. 另外,您可以使用not exists ,但是根据我对mysql查询的经验, outer join / null检查通常会胜过它。

insert into macadd
select v.mac
from savedemail s 
    join volley v on s.email = v.email
where not exists (
    select 1
    from macadd 
    where mac = v.mac
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM