开发者

multiple reference to primary key in a table in mysql

开发者 https://www.devze.com 2023-02-09 21:22 出处:网络
i trying to retrieve value by referencing primary key in multiple tables but unable to get the result.Here is the table

i trying to retrieve value by referencing primary key in multiple tables but unable to get the result.Here is the table

Density Table

 **de_id  density  color**
    1   21  red
    2   22  blue
    3   23  green
    4   24  yellow
    5   25  orange

Size Table

**si_id   length  breadth**
1        21     41
2        22     42
3        23     43
4        24     44
5        25     45

Order_de Table

**or_id  density    color  length breadth**
 1     1          2       3       4
 2     4          3       2       1

The query i am trying to work out is

  SELECT density.density,density.color,size.length,size.breadth,order_de.or_id from 
    density,size,order_de WHERE order_de.density=density.de_id and 
    order_de.color=density.de_id and order_de.length=size.si_id and 
    order_de.breadth=size.si_id  order by order_de.or_id asc

The desired result should be

density   color   length breadth  or_id
     21    blue      23    44       1
     24   green      22    41       2

But the query retrieves nothing. C开发者_高级运维an any one help??


You need to join to the tables independently for each attribute: density, color, length and breadth. Also, don't use the implicit join style. I think that contributed to your confusion in producing this query.

SELECT d1.density, d2.color, s1.length, s2.breadth, o.or_id 
    FROM Order_de o
        INNER JOIN Density d1
            ON o.density = d1.de_id
        INNER JOIN Density d2
            ON o.color= d2.de_id
        INNER JOIN Size s1
            ON o.length = s1.si_id
        INNER JOIN Size s2
            ON o.breadth = s2.si_id
    ORDER BY o.or_id asc
0

精彩评论

暂无评论...
验证码 换一张
取 消