Join two tables, merge and transpose the result

余生颓废 提交于 2019-12-24 03:25:10

问题


Ok, say i have two tables products and product_options.

structure table products be like:

   +--------------------------------------+
   | p_id | product_name | product_status |
   +--------------------------------------+
   |    1 | Keyboard ABC | PENDING        |
   |    2 | Mouse ABC    | ORDERED        |
   |    3 | Monitor ABC  | ORDERED        |
   +--------------------------------------+

And table product_options be like:

   +--------------------------------------+
   | po_id | p_id | opt_name | opt_value  +
   +--------------------------------------+
   |    1  |    1 | part_no  | KB-ABC-001 |
   |    2  |    1 | stock    |          0 |
   |    3  |    2 | part_no  | MS-ABC-001 |
   |    4  |    2 | stock    |         14 |
   |    5  |    3 | part_no  | MO-ABC-001 |
   |    6  |    3 | stock    |         10 |
   +--------------------------------------+

The question is, I want to display both tables query and has specific criteria for product that I ordered only. And the result be like:

   +------------------------------------------------------------+
   | p_id | product_name | part_no    | stock  | product_status |
   +------------------------------------------------------------+
   |    2 | Mouse ABC    | MS-ABC-001 |     14 | ORDERED        |
   |    3 | Monitor ABC  | MO-ABC-001 |     10 | ORDERED        |
   +------------------------------------------------------------+

My SQL Query is:

   SELECT 
          t.p_id, 
          t.product_name,
          (CASE WHEN t.key_name = 'part_no' THEN t.key_value ELSE NULL END) AS part_no,
          (CASE WHEN t.key_name = 'stock' THEN t.key_value ELSE NULL END) AS stock,
          t.product_status
   FROM
          (
               SELECT 
                      products.p_id as p_id,
                      products.product_name as product_name,
                      products.product_status as product_status,
                      product_options.opt_name as key_name, 
                      product_options.opt_value as key_value
               FROM 
                      product_options Inner Join 
                      products On product_options.p_id = products.p_id
               WHERE
                      (products.product_status = 'ORDERED') AND 
                      (product_options.opt_name = 'part_no' OR product_options.opt_name = 'stock')
              ) as t
   GROUP BY t.p_id

What I got is:

   +------------------------------------------------------------+
   | p_id | product_name | part_no    | stock  | product_status |
   +------------------------------------------------------------+
   |    2 | Mouse ABC    | MS-ABC-001 |   NULL | ORDERED        |
   |    3 | Monitor ABC  | MO-ABC-001 |   NULL | ORDERED        |
   +------------------------------------------------------------+

But, what I want is to display NULL at stock field as from data on product_options has:

   +------------------------------------------------------------+
   | p_id | product_name | part_no    | stock  | product_status |
   +------------------------------------------------------------+
   |    2 | Mouse ABC    | MS-ABC-001 |     14 | ORDERED        |
   |    3 | Monitor ABC  | MO-ABC-001 |     10 | ORDERED        |
   +------------------------------------------------------------+

The question is, how can I display / merge two tables with case like this? I tried to use GROUP_CONCAT, but that is not what I am searching for.

See SQLFiddle

Please help.


回答1:


Try this query :

   select p.p_id p_id , p.product_name product_name ,
   (select i.opt_value from product_options i where i.p_id=p.p_id and i.opt_name='part_no') part_no,

   (select i.opt_value from product_options i where i.p_id=p.p_id and i.opt_name='stock') stock,
   p.product_status product_status

   from products p
   where p.product_status='ORDERED';



回答2:


You can try this:

SELECT 
  p.p_id p_id , 
  p.product_name product_name , 
  po.opt_value_1 AS part_no,
  po.opt_value_2 AS stock, 
  p.product_status product_status
FROM
products p LEFT JOIN (SELECT po1.p_id,
                        po1.opt_name AS opt_name_1,
                        po1.opt_value AS opt_value_1,
                        po2.opt_name AS opt_name_2,
                        po2.opt_value AS opt_value_2
                     FROM product_options po1 
                       LEFT JOIN product_options po2
                       ON po1.p_id = po2.p_id 
                       AND po1.opt_name != po2.opt_name  
                     GROUP BY po1.p_id) AS po
  ON p.p_id = po.p_id
  WHERE p.product_status='ORDERED';

Here is the sqlfiddle.




回答3:


You can JOIN together two queries as follows to get your result table:

SELECT t1.p_id, t1.product_name, t1.part_no, t2.stock, t1.product_status
FROM
(
    SELECT products.p_id, products.product_name, products.product_status, po1.opt_value AS 'part_no'
    FROM products INNER JOIN product_options AS po1
    ON products.p_id = po1.p_id
    WHERE po1.opt_name = 'part_no'
) AS t1
INNER JOIN
(
    SELECT products.p_id, po2.opt_value AS 'stock'
    FROM products INNER JOIN product_options AS po2
    ON products.p_id = po2.p_id
    WHERE po2.opt_name = 'stock'
) AS t2
ON t1.p_id = t2.p_id;

The first SELECT (t1) obtains the part number for each product by restricting products in the product_options to only those whose opt_name is 'part_no'. And the second SELECT (t2) obtains the stock using a similar strategy.




回答4:


SELECT p.p_id
     , p.product_name
     , MAX(CASE WHEN o.opt_name = 'part_no' THEN o.opt_value END) part_no
     , MAX(CASE WHEN o.opt_name = 'stock' THEN o.opt_value END) stock
     , p.product_status
  FROM products p
  JOIN product_options o
    ON o.p_id = p.p_id
 WHERE p.product_status = 'ordered'
 GROUP
    BY p.p_id;

or, faster, but more typing...

SELECT p.p_id
     , p.product_name
     , o1.opt_value part_no
     , o2.opt_value stock
     , p.product_status
  FROM products p
  LEFT
  JOIN product_options o1
    ON o1.p_id = p.p_id
   AND o1.opt_name = 'part_no'
  LEFT
  JOIN product_options o2
    ON o2.p_id = p.p_id
   AND o2.opt_name = 'stock'
 WHERE product_status = 'ordered'


来源:https://stackoverflow.com/questions/29193028/join-two-tables-merge-and-transpose-the-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!