How can we join common tables in MySQL?
I have a complex query in Postgres which I am trying to convert in MySQL. The Postgres query has three chained queries. The first two create two common tables and the final query does a join on these two common tables. A simplified version of the query looks something like this . Is there a way to join these two common tables in MySQL ? I need the query to run for 5.6,5.7 and 8.0 so the new feature for CTE in 8.0 is not a solution.
(Select table1.y_id as year_id,
SUM(table1.total_weight) AS metric_value
from (SELECT student_name,y_id,total_weight from student_metrics where y_id>10 ) table1
group by year_id
order by metric_value DESC
limit by 5
)table2
The third query should do a join of table1 and table2 on table1.y_id = table2.year_id.
To give a high level idea of what each of the query does:
- Query 1 fetches data from the master table(Say, table 1) and stores
it in a common table based on some conditions - Query 2 groups and sorts the rows obtained in Query 1 based on a user specified column and limits it to top 'N'
- Query 3 returns all details(on table 1) of only these N unique Ids(obtained from table 2) by perfoming a join on table1.id =
table2.id
mysql sql view common-table-expression temp-tables
add a comment |
I have a complex query in Postgres which I am trying to convert in MySQL. The Postgres query has three chained queries. The first two create two common tables and the final query does a join on these two common tables. A simplified version of the query looks something like this . Is there a way to join these two common tables in MySQL ? I need the query to run for 5.6,5.7 and 8.0 so the new feature for CTE in 8.0 is not a solution.
(Select table1.y_id as year_id,
SUM(table1.total_weight) AS metric_value
from (SELECT student_name,y_id,total_weight from student_metrics where y_id>10 ) table1
group by year_id
order by metric_value DESC
limit by 5
)table2
The third query should do a join of table1 and table2 on table1.y_id = table2.year_id.
To give a high level idea of what each of the query does:
- Query 1 fetches data from the master table(Say, table 1) and stores
it in a common table based on some conditions - Query 2 groups and sorts the rows obtained in Query 1 based on a user specified column and limits it to top 'N'
- Query 3 returns all details(on table 1) of only these N unique Ids(obtained from table 2) by perfoming a join on table1.id =
table2.id
mysql sql view common-table-expression temp-tables
add a comment |
I have a complex query in Postgres which I am trying to convert in MySQL. The Postgres query has three chained queries. The first two create two common tables and the final query does a join on these two common tables. A simplified version of the query looks something like this . Is there a way to join these two common tables in MySQL ? I need the query to run for 5.6,5.7 and 8.0 so the new feature for CTE in 8.0 is not a solution.
(Select table1.y_id as year_id,
SUM(table1.total_weight) AS metric_value
from (SELECT student_name,y_id,total_weight from student_metrics where y_id>10 ) table1
group by year_id
order by metric_value DESC
limit by 5
)table2
The third query should do a join of table1 and table2 on table1.y_id = table2.year_id.
To give a high level idea of what each of the query does:
- Query 1 fetches data from the master table(Say, table 1) and stores
it in a common table based on some conditions - Query 2 groups and sorts the rows obtained in Query 1 based on a user specified column and limits it to top 'N'
- Query 3 returns all details(on table 1) of only these N unique Ids(obtained from table 2) by perfoming a join on table1.id =
table2.id
mysql sql view common-table-expression temp-tables
I have a complex query in Postgres which I am trying to convert in MySQL. The Postgres query has three chained queries. The first two create two common tables and the final query does a join on these two common tables. A simplified version of the query looks something like this . Is there a way to join these two common tables in MySQL ? I need the query to run for 5.6,5.7 and 8.0 so the new feature for CTE in 8.0 is not a solution.
(Select table1.y_id as year_id,
SUM(table1.total_weight) AS metric_value
from (SELECT student_name,y_id,total_weight from student_metrics where y_id>10 ) table1
group by year_id
order by metric_value DESC
limit by 5
)table2
The third query should do a join of table1 and table2 on table1.y_id = table2.year_id.
To give a high level idea of what each of the query does:
- Query 1 fetches data from the master table(Say, table 1) and stores
it in a common table based on some conditions - Query 2 groups and sorts the rows obtained in Query 1 based on a user specified column and limits it to top 'N'
- Query 3 returns all details(on table 1) of only these N unique Ids(obtained from table 2) by perfoming a join on table1.id =
table2.id
mysql sql view common-table-expression temp-tables
mysql sql view common-table-expression temp-tables
edited Nov 13 '18 at 2:17
Phil
96.2k11136156
96.2k11136156
asked Nov 13 '18 at 2:15
user5566364user5566364
6119
6119
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could simply repeat the table1
subquery:
select
table1.*
from
(select student_name,y_id,total_weight from student_metrics where y_id>10) as table1
inner join (
select tbl1.y_id as year_id,
sum(tbl1.total_weight) as metric_value
from
(select student_name,y_id,total_weight from student_metrics where y_id>10 ) as tbl1
group by tbl1.y_id
order by sum(tbl1.total_weight) desc
limit by 5
) as table2 on table1.y_id = table2.year_id;
Thanks for this elegant hack. Was just wondering if there is a better way to rewrite this query even more efficiently ? I am okay with alternatives to CTE's also.
– user5566364
Nov 13 '18 at 19:47
Efficiency of operation will depend on the ability of MySQL's query planner to recognize that the same operation is being done twice in this query, and actually carry it out only once. You might run EXPLAIN with this query and see what the query plan looks like. Otherwise, creating a separate view of thetable1
query and then referencing it twice in the main query probably would provide efficient execution as well as some structural simplification of the code--if the environment you're working in allows splitting this query up into two in that way.
– rd_nielsen
Nov 13 '18 at 20:16
Thanks, will look into the feasibility.
– user5566364
Nov 14 '18 at 3: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%2f53272801%2fhow-can-we-join-common-tables-in-mysql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could simply repeat the table1
subquery:
select
table1.*
from
(select student_name,y_id,total_weight from student_metrics where y_id>10) as table1
inner join (
select tbl1.y_id as year_id,
sum(tbl1.total_weight) as metric_value
from
(select student_name,y_id,total_weight from student_metrics where y_id>10 ) as tbl1
group by tbl1.y_id
order by sum(tbl1.total_weight) desc
limit by 5
) as table2 on table1.y_id = table2.year_id;
Thanks for this elegant hack. Was just wondering if there is a better way to rewrite this query even more efficiently ? I am okay with alternatives to CTE's also.
– user5566364
Nov 13 '18 at 19:47
Efficiency of operation will depend on the ability of MySQL's query planner to recognize that the same operation is being done twice in this query, and actually carry it out only once. You might run EXPLAIN with this query and see what the query plan looks like. Otherwise, creating a separate view of thetable1
query and then referencing it twice in the main query probably would provide efficient execution as well as some structural simplification of the code--if the environment you're working in allows splitting this query up into two in that way.
– rd_nielsen
Nov 13 '18 at 20:16
Thanks, will look into the feasibility.
– user5566364
Nov 14 '18 at 3:59
add a comment |
You could simply repeat the table1
subquery:
select
table1.*
from
(select student_name,y_id,total_weight from student_metrics where y_id>10) as table1
inner join (
select tbl1.y_id as year_id,
sum(tbl1.total_weight) as metric_value
from
(select student_name,y_id,total_weight from student_metrics where y_id>10 ) as tbl1
group by tbl1.y_id
order by sum(tbl1.total_weight) desc
limit by 5
) as table2 on table1.y_id = table2.year_id;
Thanks for this elegant hack. Was just wondering if there is a better way to rewrite this query even more efficiently ? I am okay with alternatives to CTE's also.
– user5566364
Nov 13 '18 at 19:47
Efficiency of operation will depend on the ability of MySQL's query planner to recognize that the same operation is being done twice in this query, and actually carry it out only once. You might run EXPLAIN with this query and see what the query plan looks like. Otherwise, creating a separate view of thetable1
query and then referencing it twice in the main query probably would provide efficient execution as well as some structural simplification of the code--if the environment you're working in allows splitting this query up into two in that way.
– rd_nielsen
Nov 13 '18 at 20:16
Thanks, will look into the feasibility.
– user5566364
Nov 14 '18 at 3:59
add a comment |
You could simply repeat the table1
subquery:
select
table1.*
from
(select student_name,y_id,total_weight from student_metrics where y_id>10) as table1
inner join (
select tbl1.y_id as year_id,
sum(tbl1.total_weight) as metric_value
from
(select student_name,y_id,total_weight from student_metrics where y_id>10 ) as tbl1
group by tbl1.y_id
order by sum(tbl1.total_weight) desc
limit by 5
) as table2 on table1.y_id = table2.year_id;
You could simply repeat the table1
subquery:
select
table1.*
from
(select student_name,y_id,total_weight from student_metrics where y_id>10) as table1
inner join (
select tbl1.y_id as year_id,
sum(tbl1.total_weight) as metric_value
from
(select student_name,y_id,total_weight from student_metrics where y_id>10 ) as tbl1
group by tbl1.y_id
order by sum(tbl1.total_weight) desc
limit by 5
) as table2 on table1.y_id = table2.year_id;
answered Nov 13 '18 at 2:48
rd_nielsenrd_nielsen
1,6152615
1,6152615
Thanks for this elegant hack. Was just wondering if there is a better way to rewrite this query even more efficiently ? I am okay with alternatives to CTE's also.
– user5566364
Nov 13 '18 at 19:47
Efficiency of operation will depend on the ability of MySQL's query planner to recognize that the same operation is being done twice in this query, and actually carry it out only once. You might run EXPLAIN with this query and see what the query plan looks like. Otherwise, creating a separate view of thetable1
query and then referencing it twice in the main query probably would provide efficient execution as well as some structural simplification of the code--if the environment you're working in allows splitting this query up into two in that way.
– rd_nielsen
Nov 13 '18 at 20:16
Thanks, will look into the feasibility.
– user5566364
Nov 14 '18 at 3:59
add a comment |
Thanks for this elegant hack. Was just wondering if there is a better way to rewrite this query even more efficiently ? I am okay with alternatives to CTE's also.
– user5566364
Nov 13 '18 at 19:47
Efficiency of operation will depend on the ability of MySQL's query planner to recognize that the same operation is being done twice in this query, and actually carry it out only once. You might run EXPLAIN with this query and see what the query plan looks like. Otherwise, creating a separate view of thetable1
query and then referencing it twice in the main query probably would provide efficient execution as well as some structural simplification of the code--if the environment you're working in allows splitting this query up into two in that way.
– rd_nielsen
Nov 13 '18 at 20:16
Thanks, will look into the feasibility.
– user5566364
Nov 14 '18 at 3:59
Thanks for this elegant hack. Was just wondering if there is a better way to rewrite this query even more efficiently ? I am okay with alternatives to CTE's also.
– user5566364
Nov 13 '18 at 19:47
Thanks for this elegant hack. Was just wondering if there is a better way to rewrite this query even more efficiently ? I am okay with alternatives to CTE's also.
– user5566364
Nov 13 '18 at 19:47
Efficiency of operation will depend on the ability of MySQL's query planner to recognize that the same operation is being done twice in this query, and actually carry it out only once. You might run EXPLAIN with this query and see what the query plan looks like. Otherwise, creating a separate view of the
table1
query and then referencing it twice in the main query probably would provide efficient execution as well as some structural simplification of the code--if the environment you're working in allows splitting this query up into two in that way.– rd_nielsen
Nov 13 '18 at 20:16
Efficiency of operation will depend on the ability of MySQL's query planner to recognize that the same operation is being done twice in this query, and actually carry it out only once. You might run EXPLAIN with this query and see what the query plan looks like. Otherwise, creating a separate view of the
table1
query and then referencing it twice in the main query probably would provide efficient execution as well as some structural simplification of the code--if the environment you're working in allows splitting this query up into two in that way.– rd_nielsen
Nov 13 '18 at 20:16
Thanks, will look into the feasibility.
– user5566364
Nov 14 '18 at 3:59
Thanks, will look into the feasibility.
– user5566364
Nov 14 '18 at 3: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%2f53272801%2fhow-can-we-join-common-tables-in-mysql%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