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.
mysql sql not-exists
add a comment |
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.
mysql sql not-exists
To me that query looks correct. Not entirely as I would write it (I would at least use two separatenot exists
, useunion all
, usetmp
in the subqueries, instead of repeatingthe values, and omitlimit 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
add a comment |
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.
mysql sql not-exists
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
mysql sql not-exists
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 separatenot exists
, useunion all
, usetmp
in the subqueries, instead of repeatingthe values, and omitlimit 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
add a comment |
To me that query looks correct. Not entirely as I would write it (I would at least use two separatenot exists
, useunion all
, usetmp
in the subqueries, instead of repeatingthe values, and omitlimit 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
add a comment |
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.
Silly me, how could I've not noticed that.
– Hexxul
Nov 10 at 12:21
add a comment |
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
);
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 useNOT EXISTS
twice withAND
. This should have the best performance.
– Gordon Linoff
Nov 10 at 13:11
add a comment |
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.
Silly me, how could I've not noticed that.
– Hexxul
Nov 10 at 12:21
add a comment |
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.
Silly me, how could I've not noticed that.
– Hexxul
Nov 10 at 12:21
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
);
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 useNOT EXISTS
twice withAND
. This should have the best performance.
– Gordon Linoff
Nov 10 at 13:11
add a comment |
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
);
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 useNOT EXISTS
twice withAND
. This should have the best performance.
– Gordon Linoff
Nov 10 at 13:11
add a comment |
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
);
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
);
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 useNOT EXISTS
twice withAND
. This should have the best performance.
– Gordon Linoff
Nov 10 at 13:11
add a comment |
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 useNOT EXISTS
twice withAND
. 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
To me that query looks correct. Not entirely as I would write it (I would at least use two separate
not exists
, useunion all
, usetmp
in the subqueries, instead of repeatingthe values, and omitlimit 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