sql以特定格式显示

时间:2017-08-19 06:24:27

标签: mysql join

我试图以特定格式显示输出。 1st, 2nd, 3rd are the product IDs and 4th row is a product itself and is a master id too for 1st, 2nd, 3rd

我要显示的格式 Master status exists in the status column and does not exist independently in any separate column

我可以使用子查询查找特定状态的数据,但不能用它来显示值

1 个答案:

答案 0 :(得分:0)

以下SQL可以提供所需的输出

SELECT      P1.PID AS PRODUCT_ID, 
            P1.STATUS AS PRODUCT_STATUS, 
            P2.PID AS MASTER_ID,
            P2.STATUS AS MASTER_STATUS 
FROM        PRODUCTS AS P1 
INNER JOIN  PRODUCTS AS P2 
ON          P1.MASTER = P2.PID

输出

PRODUCT_ID  PRODUCT_STATUS      MASTER_ID   MASTER_STATUS
----------------------------------------------------------
1234        Active              7473        Active
3457        Active              7473        Active
3685        Disabled            7473        Active