I have 3 tables in the database
first-table-name : 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
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 . if the email matches then I want to store mac of volley to mac of macadd table if it already not exists.
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.
Disclaimer: My MySQL knowledge is somewhat limited, so I can almost certainly assure you there are quicker/less-resource-intensive ways to do this; however, the above method does work in my testing.
Here's one option using the outer join / null
check approach:
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.
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
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.