Mysql
 sql >> Database >  >> RDS >> Mysql

Come SELEZIONARE su due tabelle?

Basta unirti a table2 tre volte filtrando per var_name nella clausola JOIN.

SELECT t.parent_id, 
       t.id, 
       t.name, 
       t.quality, 
       t.price, 
       c.VALUE AS color, 
       s.VALUE AS size, 
       r.VALUE AS rating 
FROM   table1 t 
       LEFT JOIN table2 c 
         ON t.parent_id = c.parent_id 
            AND c.var_name = 'color' 
       LEFT JOIN table2 s 
         ON t.parent_id = s.parent_id 
            AND s.var_name = 'size' 
       LEFT JOIN table2 r 
         ON t.parent_id = r.parent_id 
            AND r.var_name = 'rating'