Select from multiple tables - One to Many relation

狂风中的少年 提交于 2020-03-01 01:57:50

问题


I have such tables:

Table Product

[ Id | Name ]

Table Images

[ Product_Id | Url | Ordernumber]

Table Prices

[ Product_Id | Combination | Currency | Price]

Table Quantites

[ Product_Id | Combination | Quantity]


Table Product is in relation one-to-many with other tables. I need to query the table and result something like this (pseudo-array):

[
    ProductId: 1,
    Name: 'Sample product',
    Images: [
        [url, 1],
        [url, 2]
    ],
    Prices: [
        [aaa, USD, 50.00],
        [aaa, EUR, 50.00],
        [bbb, USD, 59.00],
        [bbb, EUR, 59.00]
    ],
    Quantities: [
        [aaa, 5],
        [bbb, 3]
    ]
]

The way I'm doing it now is as follows: I query all the products, list their id's, and then query each table (images,prices,quantities) with WHERE IN clause. When I have all the data I start to parse the tables in php to get desired structure.

I wonder if there is some better way to extract those data, I have many different tables like this and creating configuration parser for each of them is a bit messy and problematic. Is there a possibility that mysql will take some burden from me?

thank you


回答1:


This query will do the trick for you:

SELECT product.id, product.name, 
(SELECT group_concat(CONCAT('["',images.url, '",',  images.order_number,']')) FROM images WHERE images.product_id = product.id GROUP BY (product.id)) AS IMAGES_LIST,
(SELECT GROUP_CONCAT(CONCAT('["',prices.combination, '","', prices.currency, '",', prices.price,"]" )) FROM prices WHERE prices.product_id = product.id GROUP BY (product.id)) AS PRICE_LIST,
(SELECT GROUP_CONCAT(CONCAT('["',quantites.combination, '",',  quantites.quantity,"]")) FROM quantites WHERE quantites.product_id = product.id GROUP BY (product.id)) AS Quantity_LIST
FROM product WHERE product.id = 1
  • First get the products
  • for each one using a sub-query we get the related images, and using group concat we can get them in one field
  • same thing for the prices and quantities




回答2:


It will be simpler (and more efficient if foreign keys are set up) to use JOINs than WHERE ... IN ....

The tables could be individually queried to get the required data for Product Id = 1 like this:

SELECT i.*
FROM Product prd
INNER JOIN Images i
ON prd.Id = i.Product_Id
WHERE Product_Id = 1;

SELECT prc.*
FROM Product prd
INNER JOIN Prices prc
ON prd.Id = prc.Product_Id
WHERE Product_Id = 1;

SELECT q.*
FROM Product prd
INNER JOIN Quantities q
ON prd.Id = q.Product_Id
WHERE Product_Id = 1;



回答3:


I would suggest you to have a single result set containing all these information for a ProductID and parse it in PHP. It will make the things simpler.

SELECT prd.ProductId, prd.Name, i.Url, i.Ordernumber, prc.Combination, prc.Currency, prc.Price,q.Combination, q.Quantity FROM Product prd INNER JOIN Images i ON prd.Id = i.Product_Id INNER JOIN Prices prc ON prd.Id = prc.Product_Id INNER JOIN Quantities q ON prd.Id = q.Product_Id WHERE Product_Id = 1;



来源:https://stackoverflow.com/questions/39523580/select-from-multiple-tables-one-to-many-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!