MySQL SQL如何实现这个输出?从另一个表中选择一列

时间:2018-04-03 12:01:53

标签: mysql sql

你好,有没有办法如何使用下面的表格实现我的预期输出?我alredy有一个sql querty但它只返回

var num = 5.56789;
var n = num.toFixed(2);

我想在样式表

中选择列style_name
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

我当前的查询输出是

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

我的问题是我不知道如何实现将输出的查询

products.name       style.name
    sample            style57

3 个答案:

答案 0 :(得分:0)

您只需删除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

答案 1 :(得分:0)

解决您的问题:

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

<强>输出:

Product  Style
----------------
sample1  style1
sample1  style2
sample1  style3

点击演示链接:

  

http://sqlfiddle.com/#!9/f0460c/3

答案 2 :(得分:-1)

选择a.name,b.name 来自产品a 加入风格b 在a.id = b.id

我认为这应该做的工作