UNION query functionality and the query structure











up vote
0
down vote

favorite












What I'm trying to achieve is to insert values into a table if that value does not exist in 2 extra tables.



INSERT INTO visitor(
visitor_username,
email,
PASSWORD
)
SELECT * FROM
(
SELECT
'admin2000',
'adminemail@mail.com',
'123456'
) AS tmp
WHERE NOT EXISTS
(
SELECT
admin.admin_username,
admin.email
FROM
admin AS admin
WHERE
admin.admin_username = 'admin2000' AND admin.email =
'adminemail@mail.com'
UNION
SELECT
staff.staff_username,
staff.email
FROM
staff AS staff
WHERE
staff.staff_username = 'admin2000' AND staff.email =
'adminemail@mail.com'
)
LIMIT 1


In the WHERE NOT EXIST part when I only ask for *_username (example: admin_username or staff_username) it works well, but when I need to verify if the email exists too it does not work as intended.



Am I using WHERE NOT EXIST properly? if the username 'admin2000' and email 'adminemail@mail.com' exist on the table 'admin' and I'm trying to insert it into 'visitor' table it inserts it and it should not be doing that.










share|improve this question
























  • To me that query looks correct. Not entirely as I would write it (I would at least use two separate not exists, use union all, use tmp in the subqueries, instead of repeatingthe values, and omit limit 1), but I don't see why it wouldn't work this way... Unless there is one admin with the username 'admin2000', and another one with the email 'adminemail@mail.com'. In that case it wouldn't work. The two values have to be combined in one row if you write it like this.
    – GolezTrol
    Nov 10 at 12:12












  • it shouldnt insert (username 'admin2000' and email 'adminemail@mail.com') into the table 'visitor' because the email 'adminemail@mail.com' exist in the 'admin' table, the only thing that does not exist across the tables is the username
    – Hexxul
    Nov 10 at 12:15















up vote
0
down vote

favorite












What I'm trying to achieve is to insert values into a table if that value does not exist in 2 extra tables.



INSERT INTO visitor(
visitor_username,
email,
PASSWORD
)
SELECT * FROM
(
SELECT
'admin2000',
'adminemail@mail.com',
'123456'
) AS tmp
WHERE NOT EXISTS
(
SELECT
admin.admin_username,
admin.email
FROM
admin AS admin
WHERE
admin.admin_username = 'admin2000' AND admin.email =
'adminemail@mail.com'
UNION
SELECT
staff.staff_username,
staff.email
FROM
staff AS staff
WHERE
staff.staff_username = 'admin2000' AND staff.email =
'adminemail@mail.com'
)
LIMIT 1


In the WHERE NOT EXIST part when I only ask for *_username (example: admin_username or staff_username) it works well, but when I need to verify if the email exists too it does not work as intended.



Am I using WHERE NOT EXIST properly? if the username 'admin2000' and email 'adminemail@mail.com' exist on the table 'admin' and I'm trying to insert it into 'visitor' table it inserts it and it should not be doing that.










share|improve this question
























  • To me that query looks correct. Not entirely as I would write it (I would at least use two separate not exists, use union all, use tmp in the subqueries, instead of repeatingthe values, and omit limit 1), but I don't see why it wouldn't work this way... Unless there is one admin with the username 'admin2000', and another one with the email 'adminemail@mail.com'. In that case it wouldn't work. The two values have to be combined in one row if you write it like this.
    – GolezTrol
    Nov 10 at 12:12












  • it shouldnt insert (username 'admin2000' and email 'adminemail@mail.com') into the table 'visitor' because the email 'adminemail@mail.com' exist in the 'admin' table, the only thing that does not exist across the tables is the username
    – Hexxul
    Nov 10 at 12:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











What I'm trying to achieve is to insert values into a table if that value does not exist in 2 extra tables.



INSERT INTO visitor(
visitor_username,
email,
PASSWORD
)
SELECT * FROM
(
SELECT
'admin2000',
'adminemail@mail.com',
'123456'
) AS tmp
WHERE NOT EXISTS
(
SELECT
admin.admin_username,
admin.email
FROM
admin AS admin
WHERE
admin.admin_username = 'admin2000' AND admin.email =
'adminemail@mail.com'
UNION
SELECT
staff.staff_username,
staff.email
FROM
staff AS staff
WHERE
staff.staff_username = 'admin2000' AND staff.email =
'adminemail@mail.com'
)
LIMIT 1


In the WHERE NOT EXIST part when I only ask for *_username (example: admin_username or staff_username) it works well, but when I need to verify if the email exists too it does not work as intended.



Am I using WHERE NOT EXIST properly? if the username 'admin2000' and email 'adminemail@mail.com' exist on the table 'admin' and I'm trying to insert it into 'visitor' table it inserts it and it should not be doing that.










share|improve this question















What I'm trying to achieve is to insert values into a table if that value does not exist in 2 extra tables.



INSERT INTO visitor(
visitor_username,
email,
PASSWORD
)
SELECT * FROM
(
SELECT
'admin2000',
'adminemail@mail.com',
'123456'
) AS tmp
WHERE NOT EXISTS
(
SELECT
admin.admin_username,
admin.email
FROM
admin AS admin
WHERE
admin.admin_username = 'admin2000' AND admin.email =
'adminemail@mail.com'
UNION
SELECT
staff.staff_username,
staff.email
FROM
staff AS staff
WHERE
staff.staff_username = 'admin2000' AND staff.email =
'adminemail@mail.com'
)
LIMIT 1


In the WHERE NOT EXIST part when I only ask for *_username (example: admin_username or staff_username) it works well, but when I need to verify if the email exists too it does not work as intended.



Am I using WHERE NOT EXIST properly? if the username 'admin2000' and email 'adminemail@mail.com' exist on the table 'admin' and I'm trying to insert it into 'visitor' table it inserts it and it should not be doing that.







mysql sql not-exists






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:06









lgwilliams

129211




129211










asked Nov 10 at 12:04









Hexxul

6313




6313












  • To me that query looks correct. Not entirely as I would write it (I would at least use two separate not exists, use union all, use tmp in the subqueries, instead of repeatingthe values, and omit limit 1), but I don't see why it wouldn't work this way... Unless there is one admin with the username 'admin2000', and another one with the email 'adminemail@mail.com'. In that case it wouldn't work. The two values have to be combined in one row if you write it like this.
    – GolezTrol
    Nov 10 at 12:12












  • it shouldnt insert (username 'admin2000' and email 'adminemail@mail.com') into the table 'visitor' because the email 'adminemail@mail.com' exist in the 'admin' table, the only thing that does not exist across the tables is the username
    – Hexxul
    Nov 10 at 12:15


















  • To me that query looks correct. Not entirely as I would write it (I would at least use two separate not exists, use union all, use tmp in the subqueries, instead of repeatingthe values, and omit limit 1), but I don't see why it wouldn't work this way... Unless there is one admin with the username 'admin2000', and another one with the email 'adminemail@mail.com'. In that case it wouldn't work. The two values have to be combined in one row if you write it like this.
    – GolezTrol
    Nov 10 at 12:12












  • it shouldnt insert (username 'admin2000' and email 'adminemail@mail.com') into the table 'visitor' because the email 'adminemail@mail.com' exist in the 'admin' table, the only thing that does not exist across the tables is the username
    – Hexxul
    Nov 10 at 12:15
















To me that query looks correct. Not entirely as I would write it (I would at least use two separate not exists, use union all, use tmp in the subqueries, instead of repeatingthe values, and omit limit 1), but I don't see why it wouldn't work this way... Unless there is one admin with the username 'admin2000', and another one with the email 'adminemail@mail.com'. In that case it wouldn't work. The two values have to be combined in one row if you write it like this.
– GolezTrol
Nov 10 at 12:12






To me that query looks correct. Not entirely as I would write it (I would at least use two separate not exists, use union all, use tmp in the subqueries, instead of repeatingthe values, and omit limit 1), but I don't see why it wouldn't work this way... Unless there is one admin with the username 'admin2000', and another one with the email 'adminemail@mail.com'. In that case it wouldn't work. The two values have to be combined in one row if you write it like this.
– GolezTrol
Nov 10 at 12:12














it shouldnt insert (username 'admin2000' and email 'adminemail@mail.com') into the table 'visitor' because the email 'adminemail@mail.com' exist in the 'admin' table, the only thing that does not exist across the tables is the username
– Hexxul
Nov 10 at 12:15




it shouldnt insert (username 'admin2000' and email 'adminemail@mail.com') into the table 'visitor' because the email 'adminemail@mail.com' exist in the 'admin' table, the only thing that does not exist across the tables is the username
– Hexxul
Nov 10 at 12:15












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










The problem is the AND in the subqueries. It searches for rows that have that username AND that email. So if you have an admin called admin2000, but with a different e-mail address, that admin won't be returned by the subquery, and so the new row will be inserted.



Use OR instead of AND, and the the problem will be solved.






share|improve this answer





















  • Silly me, how could I've not noticed that.
    – Hexxul
    Nov 10 at 12:21


















up vote
0
down vote













You seem to want to be writing a query like this:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.visitor_username = t.visitor_username AND a.email = t.email
)
UNION
SELECT s.staff_username, s.email, ? as password
FROM staff s
WHERE s.staff_username = 'admin2000' AND s.email =
'adminemail@mail.com';


Note that the second subquery is missing a password, so there is an error.



This seems more concisely written using a single query:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.admin_username = t.visitor_username AND a.email = t.email
) AND
NOT EXISTS (SELECT 1
FROM staff s
WHERE s.staff_username = t.visitor_username AND s.email = t.email
);





share|improve this answer























  • it is required that the usernames and emails are unique across those 3 tables (I know its a bad desing for the database, and I've told them)
    – Hexxul
    Nov 10 at 12:13










  • @Hexxul . . . That makes more sense (I misread your query). Just use NOT EXISTS twice with AND. This should have the best performance.
    – Gordon Linoff
    Nov 10 at 13:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238758%2funion-query-functionality-and-the-query-structure%23new-answer', 'question_page');
}
);

Post as a guest
































2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The problem is the AND in the subqueries. It searches for rows that have that username AND that email. So if you have an admin called admin2000, but with a different e-mail address, that admin won't be returned by the subquery, and so the new row will be inserted.



Use OR instead of AND, and the the problem will be solved.






share|improve this answer





















  • Silly me, how could I've not noticed that.
    – Hexxul
    Nov 10 at 12:21















up vote
1
down vote



accepted










The problem is the AND in the subqueries. It searches for rows that have that username AND that email. So if you have an admin called admin2000, but with a different e-mail address, that admin won't be returned by the subquery, and so the new row will be inserted.



Use OR instead of AND, and the the problem will be solved.






share|improve this answer





















  • Silly me, how could I've not noticed that.
    – Hexxul
    Nov 10 at 12:21













up vote
1
down vote



accepted







up vote
1
down vote



accepted






The problem is the AND in the subqueries. It searches for rows that have that username AND that email. So if you have an admin called admin2000, but with a different e-mail address, that admin won't be returned by the subquery, and so the new row will be inserted.



Use OR instead of AND, and the the problem will be solved.






share|improve this answer












The problem is the AND in the subqueries. It searches for rows that have that username AND that email. So if you have an admin called admin2000, but with a different e-mail address, that admin won't be returned by the subquery, and so the new row will be inserted.



Use OR instead of AND, and the the problem will be solved.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 12:19









GolezTrol

96.8k9127170




96.8k9127170












  • Silly me, how could I've not noticed that.
    – Hexxul
    Nov 10 at 12:21


















  • Silly me, how could I've not noticed that.
    – Hexxul
    Nov 10 at 12:21
















Silly me, how could I've not noticed that.
– Hexxul
Nov 10 at 12:21




Silly me, how could I've not noticed that.
– Hexxul
Nov 10 at 12:21












up vote
0
down vote













You seem to want to be writing a query like this:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.visitor_username = t.visitor_username AND a.email = t.email
)
UNION
SELECT s.staff_username, s.email, ? as password
FROM staff s
WHERE s.staff_username = 'admin2000' AND s.email =
'adminemail@mail.com';


Note that the second subquery is missing a password, so there is an error.



This seems more concisely written using a single query:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.admin_username = t.visitor_username AND a.email = t.email
) AND
NOT EXISTS (SELECT 1
FROM staff s
WHERE s.staff_username = t.visitor_username AND s.email = t.email
);





share|improve this answer























  • it is required that the usernames and emails are unique across those 3 tables (I know its a bad desing for the database, and I've told them)
    – Hexxul
    Nov 10 at 12:13










  • @Hexxul . . . That makes more sense (I misread your query). Just use NOT EXISTS twice with AND. This should have the best performance.
    – Gordon Linoff
    Nov 10 at 13:11















up vote
0
down vote













You seem to want to be writing a query like this:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.visitor_username = t.visitor_username AND a.email = t.email
)
UNION
SELECT s.staff_username, s.email, ? as password
FROM staff s
WHERE s.staff_username = 'admin2000' AND s.email =
'adminemail@mail.com';


Note that the second subquery is missing a password, so there is an error.



This seems more concisely written using a single query:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.admin_username = t.visitor_username AND a.email = t.email
) AND
NOT EXISTS (SELECT 1
FROM staff s
WHERE s.staff_username = t.visitor_username AND s.email = t.email
);





share|improve this answer























  • it is required that the usernames and emails are unique across those 3 tables (I know its a bad desing for the database, and I've told them)
    – Hexxul
    Nov 10 at 12:13










  • @Hexxul . . . That makes more sense (I misread your query). Just use NOT EXISTS twice with AND. This should have the best performance.
    – Gordon Linoff
    Nov 10 at 13:11













up vote
0
down vote










up vote
0
down vote









You seem to want to be writing a query like this:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.visitor_username = t.visitor_username AND a.email = t.email
)
UNION
SELECT s.staff_username, s.email, ? as password
FROM staff s
WHERE s.staff_username = 'admin2000' AND s.email =
'adminemail@mail.com';


Note that the second subquery is missing a password, so there is an error.



This seems more concisely written using a single query:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.admin_username = t.visitor_username AND a.email = t.email
) AND
NOT EXISTS (SELECT 1
FROM staff s
WHERE s.staff_username = t.visitor_username AND s.email = t.email
);





share|improve this answer














You seem to want to be writing a query like this:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.visitor_username = t.visitor_username AND a.email = t.email
)
UNION
SELECT s.staff_username, s.email, ? as password
FROM staff s
WHERE s.staff_username = 'admin2000' AND s.email =
'adminemail@mail.com';


Note that the second subquery is missing a password, so there is an error.



This seems more concisely written using a single query:



INSERT INTO visitor (visitor_username, email, PASSWORD)
SELECT t.*
FROM (SELECT 'admin2000' as visitor_username, 'adminemail@mail.com' as email, '123456' as PASSWORD
) t
WHERE NOT EXISTS (SELECT 1
FROM admin a
WHERE a.admin_username = t.visitor_username AND a.email = t.email
) AND
NOT EXISTS (SELECT 1
FROM staff s
WHERE s.staff_username = t.visitor_username AND s.email = t.email
);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 13:10

























answered Nov 10 at 12:11









Gordon Linoff

741k32285389




741k32285389












  • it is required that the usernames and emails are unique across those 3 tables (I know its a bad desing for the database, and I've told them)
    – Hexxul
    Nov 10 at 12:13










  • @Hexxul . . . That makes more sense (I misread your query). Just use NOT EXISTS twice with AND. This should have the best performance.
    – Gordon Linoff
    Nov 10 at 13:11


















  • it is required that the usernames and emails are unique across those 3 tables (I know its a bad desing for the database, and I've told them)
    – Hexxul
    Nov 10 at 12:13










  • @Hexxul . . . That makes more sense (I misread your query). Just use NOT EXISTS twice with AND. This should have the best performance.
    – Gordon Linoff
    Nov 10 at 13:11
















it is required that the usernames and emails are unique across those 3 tables (I know its a bad desing for the database, and I've told them)
– Hexxul
Nov 10 at 12:13




it is required that the usernames and emails are unique across those 3 tables (I know its a bad desing for the database, and I've told them)
– Hexxul
Nov 10 at 12:13












@Hexxul . . . That makes more sense (I misread your query). Just use NOT EXISTS twice with AND. This should have the best performance.
– Gordon Linoff
Nov 10 at 13:11




@Hexxul . . . That makes more sense (I misread your query). Just use NOT EXISTS twice with AND. This should have the best performance.
– Gordon Linoff
Nov 10 at 13:11


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238758%2funion-query-functionality-and-the-query-structure%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ