I am using this query:
SELECT a.sales_id, d.bus_title, a.cat_id
FROM tbl_sales a
INNER JOIN tb_category b ON a.cat_id = b.cat_id
INNER JOIN tbl_business d ON d.bus_id = a.bus_id
which produces this result:
sales_id | bus_title |cat_id
----------|----------------|------------
1 | Business 1 | 6
2 | Business 12 | 12
3 | Business 123 | 25
I changed the field cat_id into a new table named tb_sales_category which contains the fields sales_category_id, sales_id, cat_id. How can I write the new query by joining this table too to, get the same result as above?
I am kind of new to databases, need help. Thanks in advance
Try this:
The idea is fairly simple, the first field in your new table
tb_sales_categorywhich issales_category_idis working as a surrogate key, it has nothing to do with the relations between the two other tables. Then we come to the other two fields which aresales_id,cat_id, these what you should map to the other two sides of the relations.You can’t
Join tb_category b ON a.cat_id = b.cat_idon the new schema becouse we no longer havea.cat_id, and here comes the new tabletb_sales_categoryrole, by inserting it with two binding sides, one withINNER JOIN tb_category b ON s.cat_id = b.cat_idand the other withINNER JOIN tb_sales_category s ON a.sales_id = s.sales_idwe should be done.Hope this makes sense.