Mysql unknown column in subquery works
There are three tables - mapping_hospital_procedure
, master_hospital
and master_procedure
.
master_hospital
contains an id
column to identify each hospital.
master_procedure
also contains an id
column for the same purpose, i.e, to identify each procedure.
mapping_hospital_procedure
is used to map hospitals to medical procedures and contains columns hid
(hospital id) and pid
(procedure id) besides other columns. Now, if I run this query :
SELECT hid FROM master_procedure
it doesn't work as you would because there is no hid
column in master_procedure
. But if I put this query in a sub-query, it works fine. Like this way, for example -
SELECT hid FROM mapping_hospital_procedure WHERE pid = (SELECT hid FROM master_procedure)
But it shouldn't work as there is no column named hid
in master_procedure
. It should give the same Unknown column error as is the case with the previous query. Can anyone explain why or how this works?
Thank you.
mysql sql subquery
add a comment |
There are three tables - mapping_hospital_procedure
, master_hospital
and master_procedure
.
master_hospital
contains an id
column to identify each hospital.
master_procedure
also contains an id
column for the same purpose, i.e, to identify each procedure.
mapping_hospital_procedure
is used to map hospitals to medical procedures and contains columns hid
(hospital id) and pid
(procedure id) besides other columns. Now, if I run this query :
SELECT hid FROM master_procedure
it doesn't work as you would because there is no hid
column in master_procedure
. But if I put this query in a sub-query, it works fine. Like this way, for example -
SELECT hid FROM mapping_hospital_procedure WHERE pid = (SELECT hid FROM master_procedure)
But it shouldn't work as there is no column named hid
in master_procedure
. It should give the same Unknown column error as is the case with the previous query. Can anyone explain why or how this works?
Thank you.
mysql sql subquery
1
dba.stackexchange.com/questions/218871/…
– Shadow
Nov 28 '18 at 6:57
add a comment |
There are three tables - mapping_hospital_procedure
, master_hospital
and master_procedure
.
master_hospital
contains an id
column to identify each hospital.
master_procedure
also contains an id
column for the same purpose, i.e, to identify each procedure.
mapping_hospital_procedure
is used to map hospitals to medical procedures and contains columns hid
(hospital id) and pid
(procedure id) besides other columns. Now, if I run this query :
SELECT hid FROM master_procedure
it doesn't work as you would because there is no hid
column in master_procedure
. But if I put this query in a sub-query, it works fine. Like this way, for example -
SELECT hid FROM mapping_hospital_procedure WHERE pid = (SELECT hid FROM master_procedure)
But it shouldn't work as there is no column named hid
in master_procedure
. It should give the same Unknown column error as is the case with the previous query. Can anyone explain why or how this works?
Thank you.
mysql sql subquery
There are three tables - mapping_hospital_procedure
, master_hospital
and master_procedure
.
master_hospital
contains an id
column to identify each hospital.
master_procedure
also contains an id
column for the same purpose, i.e, to identify each procedure.
mapping_hospital_procedure
is used to map hospitals to medical procedures and contains columns hid
(hospital id) and pid
(procedure id) besides other columns. Now, if I run this query :
SELECT hid FROM master_procedure
it doesn't work as you would because there is no hid
column in master_procedure
. But if I put this query in a sub-query, it works fine. Like this way, for example -
SELECT hid FROM mapping_hospital_procedure WHERE pid = (SELECT hid FROM master_procedure)
But it shouldn't work as there is no column named hid
in master_procedure
. It should give the same Unknown column error as is the case with the previous query. Can anyone explain why or how this works?
Thank you.
mysql sql subquery
mysql sql subquery
asked Nov 28 '18 at 6:28
Mayank KumarMayank Kumar
3973615
3973615
1
dba.stackexchange.com/questions/218871/…
– Shadow
Nov 28 '18 at 6:57
add a comment |
1
dba.stackexchange.com/questions/218871/…
– Shadow
Nov 28 '18 at 6:57
1
1
dba.stackexchange.com/questions/218871/…
– Shadow
Nov 28 '18 at 6:57
dba.stackexchange.com/questions/218871/…
– Shadow
Nov 28 '18 at 6:57
add a comment |
2 Answers
2
active
oldest
votes
Scope issue. You sub-query has access to the main-query's columns.
Since hid isn't found in the sub-query table, it will step out and look for it in the main query - there it's found in mapping_hospital_procedure table.
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is the same as
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
Lesson now learned: Always qualify your columns!
EDIT: " This should at least be a logical error giving incorrect output, right?"
Yes, that query doesn't make much sense.
If the master_procedure table contains no rows at all, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
will be false. No rows at all are returned.
If the master_procedure table contains 2 rows or more, the "Subquery returns more than 1 row" exception should be raised. (Since it's a =
comparison. Use IN
to avoid this problem.)
If the master_procedure table contains exactly 1 row, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
condition evaluates to true if mhp.pid is not null, and the row is returned from mapping_hospital_procedure. If mhp.pid is null, the row isn't returned.
What you really want to do is probably:
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid IN (SELECT mp.id FROM master_procedure mp)
So, in the subquery here, how is mhp.hid being accessed from master_procedure since its values don't exist in that table. This should at least be a logical error giving incorrect output, right?
– Mayank Kumar
Nov 28 '18 at 7:16
Too long answer for a comment, will edit my answer.
– jarlh
Nov 28 '18 at 7:22
add a comment |
With that query you are actually doing a natural join. https://www.w3resource.com/sql/joins/natural-join.php
The database parser is smart enough to translate your query into a join.
Your query:
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is basically the same as:
SELECT hid FROM mapping_hospital_procedure
NATURAL JOIN master_procedure
WHERE pid = hid
1
I do not think that this is a good answer. First of all, any optimisation by the engine is done after checking the syntax, not before. Secondly, natural join requires identically named columns in both tables, which do not exists here. The answer from the dba site seems to be more accurate.
– Shadow
Nov 28 '18 at 6:59
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513386%2fmysql-unknown-column-in-subquery-works%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Scope issue. You sub-query has access to the main-query's columns.
Since hid isn't found in the sub-query table, it will step out and look for it in the main query - there it's found in mapping_hospital_procedure table.
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is the same as
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
Lesson now learned: Always qualify your columns!
EDIT: " This should at least be a logical error giving incorrect output, right?"
Yes, that query doesn't make much sense.
If the master_procedure table contains no rows at all, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
will be false. No rows at all are returned.
If the master_procedure table contains 2 rows or more, the "Subquery returns more than 1 row" exception should be raised. (Since it's a =
comparison. Use IN
to avoid this problem.)
If the master_procedure table contains exactly 1 row, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
condition evaluates to true if mhp.pid is not null, and the row is returned from mapping_hospital_procedure. If mhp.pid is null, the row isn't returned.
What you really want to do is probably:
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid IN (SELECT mp.id FROM master_procedure mp)
So, in the subquery here, how is mhp.hid being accessed from master_procedure since its values don't exist in that table. This should at least be a logical error giving incorrect output, right?
– Mayank Kumar
Nov 28 '18 at 7:16
Too long answer for a comment, will edit my answer.
– jarlh
Nov 28 '18 at 7:22
add a comment |
Scope issue. You sub-query has access to the main-query's columns.
Since hid isn't found in the sub-query table, it will step out and look for it in the main query - there it's found in mapping_hospital_procedure table.
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is the same as
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
Lesson now learned: Always qualify your columns!
EDIT: " This should at least be a logical error giving incorrect output, right?"
Yes, that query doesn't make much sense.
If the master_procedure table contains no rows at all, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
will be false. No rows at all are returned.
If the master_procedure table contains 2 rows or more, the "Subquery returns more than 1 row" exception should be raised. (Since it's a =
comparison. Use IN
to avoid this problem.)
If the master_procedure table contains exactly 1 row, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
condition evaluates to true if mhp.pid is not null, and the row is returned from mapping_hospital_procedure. If mhp.pid is null, the row isn't returned.
What you really want to do is probably:
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid IN (SELECT mp.id FROM master_procedure mp)
So, in the subquery here, how is mhp.hid being accessed from master_procedure since its values don't exist in that table. This should at least be a logical error giving incorrect output, right?
– Mayank Kumar
Nov 28 '18 at 7:16
Too long answer for a comment, will edit my answer.
– jarlh
Nov 28 '18 at 7:22
add a comment |
Scope issue. You sub-query has access to the main-query's columns.
Since hid isn't found in the sub-query table, it will step out and look for it in the main query - there it's found in mapping_hospital_procedure table.
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is the same as
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
Lesson now learned: Always qualify your columns!
EDIT: " This should at least be a logical error giving incorrect output, right?"
Yes, that query doesn't make much sense.
If the master_procedure table contains no rows at all, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
will be false. No rows at all are returned.
If the master_procedure table contains 2 rows or more, the "Subquery returns more than 1 row" exception should be raised. (Since it's a =
comparison. Use IN
to avoid this problem.)
If the master_procedure table contains exactly 1 row, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
condition evaluates to true if mhp.pid is not null, and the row is returned from mapping_hospital_procedure. If mhp.pid is null, the row isn't returned.
What you really want to do is probably:
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid IN (SELECT mp.id FROM master_procedure mp)
Scope issue. You sub-query has access to the main-query's columns.
Since hid isn't found in the sub-query table, it will step out and look for it in the main query - there it's found in mapping_hospital_procedure table.
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is the same as
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
Lesson now learned: Always qualify your columns!
EDIT: " This should at least be a logical error giving incorrect output, right?"
Yes, that query doesn't make much sense.
If the master_procedure table contains no rows at all, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
will be false. No rows at all are returned.
If the master_procedure table contains 2 rows or more, the "Subquery returns more than 1 row" exception should be raised. (Since it's a =
comparison. Use IN
to avoid this problem.)
If the master_procedure table contains exactly 1 row, the mhp.pid = (SELECT mhp.hid FROM master_procedure mp)
condition evaluates to true if mhp.pid is not null, and the row is returned from mapping_hospital_procedure. If mhp.pid is null, the row isn't returned.
What you really want to do is probably:
SELECT mhp.hid FROM mapping_hospital_procedure mhp
WHERE mhp.pid IN (SELECT mp.id FROM master_procedure mp)
edited Nov 28 '18 at 7:31
answered Nov 28 '18 at 6:58
jarlhjarlh
29.7k52138
29.7k52138
So, in the subquery here, how is mhp.hid being accessed from master_procedure since its values don't exist in that table. This should at least be a logical error giving incorrect output, right?
– Mayank Kumar
Nov 28 '18 at 7:16
Too long answer for a comment, will edit my answer.
– jarlh
Nov 28 '18 at 7:22
add a comment |
So, in the subquery here, how is mhp.hid being accessed from master_procedure since its values don't exist in that table. This should at least be a logical error giving incorrect output, right?
– Mayank Kumar
Nov 28 '18 at 7:16
Too long answer for a comment, will edit my answer.
– jarlh
Nov 28 '18 at 7:22
So, in the subquery here, how is mhp.hid being accessed from master_procedure since its values don't exist in that table. This should at least be a logical error giving incorrect output, right?
– Mayank Kumar
Nov 28 '18 at 7:16
So, in the subquery here, how is mhp.hid being accessed from master_procedure since its values don't exist in that table. This should at least be a logical error giving incorrect output, right?
– Mayank Kumar
Nov 28 '18 at 7:16
Too long answer for a comment, will edit my answer.
– jarlh
Nov 28 '18 at 7:22
Too long answer for a comment, will edit my answer.
– jarlh
Nov 28 '18 at 7:22
add a comment |
With that query you are actually doing a natural join. https://www.w3resource.com/sql/joins/natural-join.php
The database parser is smart enough to translate your query into a join.
Your query:
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is basically the same as:
SELECT hid FROM mapping_hospital_procedure
NATURAL JOIN master_procedure
WHERE pid = hid
1
I do not think that this is a good answer. First of all, any optimisation by the engine is done after checking the syntax, not before. Secondly, natural join requires identically named columns in both tables, which do not exists here. The answer from the dba site seems to be more accurate.
– Shadow
Nov 28 '18 at 6:59
add a comment |
With that query you are actually doing a natural join. https://www.w3resource.com/sql/joins/natural-join.php
The database parser is smart enough to translate your query into a join.
Your query:
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is basically the same as:
SELECT hid FROM mapping_hospital_procedure
NATURAL JOIN master_procedure
WHERE pid = hid
1
I do not think that this is a good answer. First of all, any optimisation by the engine is done after checking the syntax, not before. Secondly, natural join requires identically named columns in both tables, which do not exists here. The answer from the dba site seems to be more accurate.
– Shadow
Nov 28 '18 at 6:59
add a comment |
With that query you are actually doing a natural join. https://www.w3resource.com/sql/joins/natural-join.php
The database parser is smart enough to translate your query into a join.
Your query:
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is basically the same as:
SELECT hid FROM mapping_hospital_procedure
NATURAL JOIN master_procedure
WHERE pid = hid
With that query you are actually doing a natural join. https://www.w3resource.com/sql/joins/natural-join.php
The database parser is smart enough to translate your query into a join.
Your query:
SELECT hid FROM mapping_hospital_procedure
WHERE pid = (SELECT hid FROM master_procedure)
Is basically the same as:
SELECT hid FROM mapping_hospital_procedure
NATURAL JOIN master_procedure
WHERE pid = hid
answered Nov 28 '18 at 6:38
Mr.TurtleMr.Turtle
1,13821328
1,13821328
1
I do not think that this is a good answer. First of all, any optimisation by the engine is done after checking the syntax, not before. Secondly, natural join requires identically named columns in both tables, which do not exists here. The answer from the dba site seems to be more accurate.
– Shadow
Nov 28 '18 at 6:59
add a comment |
1
I do not think that this is a good answer. First of all, any optimisation by the engine is done after checking the syntax, not before. Secondly, natural join requires identically named columns in both tables, which do not exists here. The answer from the dba site seems to be more accurate.
– Shadow
Nov 28 '18 at 6:59
1
1
I do not think that this is a good answer. First of all, any optimisation by the engine is done after checking the syntax, not before. Secondly, natural join requires identically named columns in both tables, which do not exists here. The answer from the dba site seems to be more accurate.
– Shadow
Nov 28 '18 at 6:59
I do not think that this is a good answer. First of all, any optimisation by the engine is done after checking the syntax, not before. Secondly, natural join requires identically named columns in both tables, which do not exists here. The answer from the dba site seems to be more accurate.
– Shadow
Nov 28 '18 at 6:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513386%2fmysql-unknown-column-in-subquery-works%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
dba.stackexchange.com/questions/218871/…
– Shadow
Nov 28 '18 at 6:57