I have this query
SELECT *, bp.product_id
FROM builder_product as bp
JOIN builder_step as bs
ON bs.builder_id=bp.builder_id
JOIN builder as b
ON b.builder_id=bs.builder_id
WHERE b.business_id = '60'
GROUP BY product_id
ORDER BY bs.step_number
which is grabbing all the products from the builder_product table and all looks ok but on closer look the data is missing a few records and all the names are the same.
Here is the data and the schema and basically what I want is a query that gives me all the products from the builder_product table and the corresponding name from the builder_step table with the business_id of 60 from the builder table.
Any ideas what I am doing wrong and how to avoid duplicates?
It isn’t clear from your schema whether
builder_productandbuilder_stepare related bybuilder_idor bybuilder_step_id. If you clarify that I’ll modify the answer below.You aren’t performing any aggregate functions (
SUM(), COUNT(), AVG()) so you have no need for aGROUP BY. Instead you just have a couple ofJOINs to perform:The duplicates are a result of attempting to
SELECT *without specifying a table name/alias. Instead,SELECT bp.*, bs.nameindicates that specifically all columns frombuilder_productshould be returned, plus thebuilder_namecolumn frombuilder_step.