Parent Child table record - Building SQL query

后端 未结 1 705
野性不改
野性不改 2021-01-29 02:19

Here is my table and data of these tables

Table name: Code

CID Code
1   abc
2   def
3   xyz

Table Name : Detai

1条回答
  •  时光说笑
    2021-01-29 02:43

    Sounds like you're looking for the cartesian product:

    SELECT
        c.CID * d.ID AS ID,
        c.Code,
        CASE
            WHEN c.CID = d.CID THEN d.Name
            ELSE NULL
        END AS Name
    FROM Code c
    CROSS JOIN Details d
    

    Although cartesian products are quite slow for larger tables... so be sure that this is what you really want.

    0 讨论(0)
提交回复
热议问题