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

Interroga i dati da 2 tabelle MySQL con alcuni record duplicati

puoi usare union se hai bisogno di risultati distinti per entrambi o union all se hai bisogno anche di risultati duplicati

      SELECT CustID
          , DateSubmitted
          , Type
          , Points
              FROM `trans_summary`
                WHERE CustID = '10009'
    UNION

    SELECT CustID
    , DateSubmitted
    , Type
    , PointTotal 
        FROM `ptrans_detail` 
           WHERE CustID = '10009'
                and DateSubmitted NOT IN 
               (SELECT DateSubmitted FROM 
                 `trans_summary` 
                  WHERE CustID = '10009')

o unisci tutto se necessario anche il risultato duplicato

      SELECT CustID
          , DateSubmitted
          , Type
          , Points
              FROM `trans_summary`
                WHERE CustID = '10009'
    UNION ALL 

    SELECT CustID
    , DateSubmitted
    , Type
    , PointTotal 
        FROM `ptrans_detail` 
           WHERE CustID = '10009'
                and DateSubmitted NOT IN 
               (SELECT DateSubmitted FROM 
                 `trans_summary` 
                  WHERE CustID = '10009')