Duplicate columns with inner Join

后端 未结 2 983
旧时难觅i
旧时难觅i 2020-12-19 01:34
SELECT 
    dealing_record.*
    ,shares.*
    ,transaction_type.*
FROM 
    shares 
    INNER JOIN shares ON shares.share_ID = dealing_record.share_id
    INNER JOI         


        
相关标签:
2条回答
  • 2020-12-19 01:58

    You have duplicate columns, because, you're asking to the SQL engine for columns that they will show you the same data (with SELECT dealing_record.* and so on) , and then duplicates.

    For example, the transaction_type.transaction_type_id column and the dealing_record.transaction_type_id column will have matching rows (otherwise you won't see anything with an INNER JOIN) and you will see those duplicates.

    If you want to avoid this problem or, at least, to reduce the risk of having duplicates in your results, improve your query, using only the columns you really need, as @ConradFrix already said. An example would be this:

    SELECT 
        dealing_record.Name
        ,shares.ID
        ,shares.Name
        ,transaction_type.Name
        ,transaction_type.ID
    FROM 
        shares 
        INNER JOIN shares ON shares.share_ID = dealing_record.share_id
        INNER JOIN transaction_type ON transaction_type.transaction_type_id = dealing_record.transaction_type_id;
    
    0 讨论(0)
  • 2020-12-19 02:01

    Try to join shares with dealing_record, not shares again:

    select dealing_record.*,
           shares.*,
           transaction_type.*
    FROM shares inner join dealing_record on shares.share_ID = dealing_record.share_id
                inner join transaction_type on transaction_type.transaction_type_id=
    dealing_record.transaction_type_id;
    
    0 讨论(0)
提交回复
热议问题