Hello is there a way how to achieve my expected output below using those tables below? I alredy have a sql querty but it only returns
Products table
id name
1 sample1
2 sample2
3 sample3
product_style table
product_id style_id
1 1
1 2
1 3
schools table
id name
1 school1
2 school2
3 school3
style table
id school_id name
1 1 style1
2 1 style2
3 2 style3
4 2 style4
5 57 style57
school_products table
school_id product_id
1 1
1 2
1 3
I want to select the column style_name in style table
SELECT from prodcuts.name, style.name
FROM school_products
LEFT JOIN products on school_products.product_id = products.id
LEFT JOIN schools on school_products.id = schools.id
LEFT JOIN styles on school_products.id = styles.schools_id
WHERE schools.id = 57
my current query output is
products.name style.name
sample style57
and my problem is I dont how the achieve a query that will output
products.name style.name
sample1 style1
sample1 style2
sample1 style3
you just need to delete the Where schools.id = 75
SELECT from prodcuts.name, style.name
FROM school_products
LEFT JOIN products on school_products.product_id = products.id
LEFT JOIN schools on school_products.school_id = schools.id
LEFT JOIN styles on school_products.school_id = styles.schools_id
Solution to your problem:
SELECT p.name Product, s.name Style
FROM products p
INNER JOIN product_style ps
ON ps.product_id = p.id
INNER JOIN style s
ON s.id = ps.style_id
OUTPUT:
Product Style
----------------
sample1 style1
sample1 style2
sample1 style3
Follow the link to the demo:
select a.name, b.name from products a join style b on a.id=b.id
I think this should do the work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.