MySQL SELECT and JSON format products from multiple sources











up vote
0
down vote

favorite












I'm learning SQL in the middle of an internship I'm doing and just wrote my first (at least what I would call) complex query. The query has to return information from a relational DB about all product catalogs from a company and the products that they contain in a json format, which can reference products through a simple many to many relationship of:



[catalogs] <-> [products]


AND/OR through



[catalogs] <-> [product groups] <-> [products]


I'm new to SQL and using MySQL 5.7.14. I don't know if this is particularly optimized or at all good practice so those are my biggest concerns with my query.
There might also be some redundant JOINs because I'm not entirely comfortable with the scoping of subqueries.



SELECT
cat.*,
CONCAT('[',
GROUP_CONCAT(CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS products,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', pg.id,
', "name":"', IFNULL(pg.name, ''),
'", "products": ', IFNULL(pg.product_group_product_list, ''),
'}')
ORDER BY pg.id),
']') AS product_groups
FROM
companies AS c
LEFT JOIN
catalogs AS cat ON cat.company_id = c.id
LEFT JOIN
catalog_products AS cpjoin ON cpjoin.catalog_id = cat.id
LEFT JOIN
products AS p ON p.id = cpjoin.product_id
LEFT JOIN
catalog_product_groups AS cpgjoin ON cpgjoin.catalog_id = cat.id
LEFT JOIN (
SELECT
pg2.*,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS product_group_product_list
FROM
companies AS c2
LEFT JOIN
catalogs AS cat2 ON cat2.company_id = c2.id
LEFT JOIN
catalog_product_groups AS cpgjoin2 ON cpgjoin2.catalog_id = cat2.id
LEFT JOIN
product_groups AS pg2 ON pg2.id = cpgjoin2.product_group_id
LEFT JOIN
product_group_products AS pgjoin2 ON pgjoin2.product_group_id = pg2.id
LEFT JOIN
products p ON p.id = pgjoin2.product_id
GROUP BY
pg2.id
) AS pg ON pg.id = cpgjoin.product_group_id
WHERE
c.id = 1
GROUP BY cat.id;









share|improve this question









New contributor




Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Welcome to Code Review! You haven't shown the definition of your table(s), without which it's hard to give a good answer. I recommend you include these definitions (preferably as SQL statements, so that reviewers can reproduce your test environment).
    – Toby Speight
    yesterday















up vote
0
down vote

favorite












I'm learning SQL in the middle of an internship I'm doing and just wrote my first (at least what I would call) complex query. The query has to return information from a relational DB about all product catalogs from a company and the products that they contain in a json format, which can reference products through a simple many to many relationship of:



[catalogs] <-> [products]


AND/OR through



[catalogs] <-> [product groups] <-> [products]


I'm new to SQL and using MySQL 5.7.14. I don't know if this is particularly optimized or at all good practice so those are my biggest concerns with my query.
There might also be some redundant JOINs because I'm not entirely comfortable with the scoping of subqueries.



SELECT
cat.*,
CONCAT('[',
GROUP_CONCAT(CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS products,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', pg.id,
', "name":"', IFNULL(pg.name, ''),
'", "products": ', IFNULL(pg.product_group_product_list, ''),
'}')
ORDER BY pg.id),
']') AS product_groups
FROM
companies AS c
LEFT JOIN
catalogs AS cat ON cat.company_id = c.id
LEFT JOIN
catalog_products AS cpjoin ON cpjoin.catalog_id = cat.id
LEFT JOIN
products AS p ON p.id = cpjoin.product_id
LEFT JOIN
catalog_product_groups AS cpgjoin ON cpgjoin.catalog_id = cat.id
LEFT JOIN (
SELECT
pg2.*,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS product_group_product_list
FROM
companies AS c2
LEFT JOIN
catalogs AS cat2 ON cat2.company_id = c2.id
LEFT JOIN
catalog_product_groups AS cpgjoin2 ON cpgjoin2.catalog_id = cat2.id
LEFT JOIN
product_groups AS pg2 ON pg2.id = cpgjoin2.product_group_id
LEFT JOIN
product_group_products AS pgjoin2 ON pgjoin2.product_group_id = pg2.id
LEFT JOIN
products p ON p.id = pgjoin2.product_id
GROUP BY
pg2.id
) AS pg ON pg.id = cpgjoin.product_group_id
WHERE
c.id = 1
GROUP BY cat.id;









share|improve this question









New contributor




Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Welcome to Code Review! You haven't shown the definition of your table(s), without which it's hard to give a good answer. I recommend you include these definitions (preferably as SQL statements, so that reviewers can reproduce your test environment).
    – Toby Speight
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm learning SQL in the middle of an internship I'm doing and just wrote my first (at least what I would call) complex query. The query has to return information from a relational DB about all product catalogs from a company and the products that they contain in a json format, which can reference products through a simple many to many relationship of:



[catalogs] <-> [products]


AND/OR through



[catalogs] <-> [product groups] <-> [products]


I'm new to SQL and using MySQL 5.7.14. I don't know if this is particularly optimized or at all good practice so those are my biggest concerns with my query.
There might also be some redundant JOINs because I'm not entirely comfortable with the scoping of subqueries.



SELECT
cat.*,
CONCAT('[',
GROUP_CONCAT(CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS products,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', pg.id,
', "name":"', IFNULL(pg.name, ''),
'", "products": ', IFNULL(pg.product_group_product_list, ''),
'}')
ORDER BY pg.id),
']') AS product_groups
FROM
companies AS c
LEFT JOIN
catalogs AS cat ON cat.company_id = c.id
LEFT JOIN
catalog_products AS cpjoin ON cpjoin.catalog_id = cat.id
LEFT JOIN
products AS p ON p.id = cpjoin.product_id
LEFT JOIN
catalog_product_groups AS cpgjoin ON cpgjoin.catalog_id = cat.id
LEFT JOIN (
SELECT
pg2.*,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS product_group_product_list
FROM
companies AS c2
LEFT JOIN
catalogs AS cat2 ON cat2.company_id = c2.id
LEFT JOIN
catalog_product_groups AS cpgjoin2 ON cpgjoin2.catalog_id = cat2.id
LEFT JOIN
product_groups AS pg2 ON pg2.id = cpgjoin2.product_group_id
LEFT JOIN
product_group_products AS pgjoin2 ON pgjoin2.product_group_id = pg2.id
LEFT JOIN
products p ON p.id = pgjoin2.product_id
GROUP BY
pg2.id
) AS pg ON pg.id = cpgjoin.product_group_id
WHERE
c.id = 1
GROUP BY cat.id;









share|improve this question









New contributor




Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm learning SQL in the middle of an internship I'm doing and just wrote my first (at least what I would call) complex query. The query has to return information from a relational DB about all product catalogs from a company and the products that they contain in a json format, which can reference products through a simple many to many relationship of:



[catalogs] <-> [products]


AND/OR through



[catalogs] <-> [product groups] <-> [products]


I'm new to SQL and using MySQL 5.7.14. I don't know if this is particularly optimized or at all good practice so those are my biggest concerns with my query.
There might also be some redundant JOINs because I'm not entirely comfortable with the scoping of subqueries.



SELECT
cat.*,
CONCAT('[',
GROUP_CONCAT(CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS products,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', pg.id,
', "name":"', IFNULL(pg.name, ''),
'", "products": ', IFNULL(pg.product_group_product_list, ''),
'}')
ORDER BY pg.id),
']') AS product_groups
FROM
companies AS c
LEFT JOIN
catalogs AS cat ON cat.company_id = c.id
LEFT JOIN
catalog_products AS cpjoin ON cpjoin.catalog_id = cat.id
LEFT JOIN
products AS p ON p.id = cpjoin.product_id
LEFT JOIN
catalog_product_groups AS cpgjoin ON cpgjoin.catalog_id = cat.id
LEFT JOIN (
SELECT
pg2.*,
CONCAT('[',
GROUP_CONCAT(DISTINCT CONCAT(
'{"id":', p.id,
', "name":"', IFNULL(p.name, ''),
'", "image_url":"', IFNULL(p.image_url, ''),
'", "description":"', IFNULL(p.description, ''),
'"}')
ORDER BY p.id),
']') AS product_group_product_list
FROM
companies AS c2
LEFT JOIN
catalogs AS cat2 ON cat2.company_id = c2.id
LEFT JOIN
catalog_product_groups AS cpgjoin2 ON cpgjoin2.catalog_id = cat2.id
LEFT JOIN
product_groups AS pg2 ON pg2.id = cpgjoin2.product_group_id
LEFT JOIN
product_group_products AS pgjoin2 ON pgjoin2.product_group_id = pg2.id
LEFT JOIN
products p ON p.id = pgjoin2.product_id
GROUP BY
pg2.id
) AS pg ON pg.id = cpgjoin.product_group_id
WHERE
c.id = 1
GROUP BY cat.id;






sql mysql






share|improve this question









New contributor




Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Jamal

30.2k11115226




30.2k11115226






New contributor




Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Caden McCauley

1




1




New contributor




Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Caden McCauley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Welcome to Code Review! You haven't shown the definition of your table(s), without which it's hard to give a good answer. I recommend you include these definitions (preferably as SQL statements, so that reviewers can reproduce your test environment).
    – Toby Speight
    yesterday














  • 1




    Welcome to Code Review! You haven't shown the definition of your table(s), without which it's hard to give a good answer. I recommend you include these definitions (preferably as SQL statements, so that reviewers can reproduce your test environment).
    – Toby Speight
    yesterday








1




1




Welcome to Code Review! You haven't shown the definition of your table(s), without which it's hard to give a good answer. I recommend you include these definitions (preferably as SQL statements, so that reviewers can reproduce your test environment).
– Toby Speight
yesterday




Welcome to Code Review! You haven't shown the definition of your table(s), without which it's hard to give a good answer. I recommend you include these definitions (preferably as SQL statements, so that reviewers can reproduce your test environment).
– Toby Speight
yesterday















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});






Caden McCauley is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207692%2fmysql-select-and-json-format-products-from-multiple-sources%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Caden McCauley is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Caden McCauley is a new contributor. Be nice, and check out our Code of Conduct.













Caden McCauley is a new contributor. Be nice, and check out our Code of Conduct.












Caden McCauley is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207692%2fmysql-select-and-json-format-products-from-multiple-sources%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei