连接两个表并避免重复行

时间:2016-10-17 16:23:50

标签: sql-server-2014

我在sql server中遇到了以下逻辑。

表1:

ID  Requestid  
1    0001       
2    0004       
3   0004        
1   0005  

表2

parentID  Requestid  Age
1         0001       29
2         0004       30
3         0004        34
1         0005        27

查询:

select * from table1 t1
join table t2
on t2.parentid =t1.id

当我加入这些表格时,我的结果低于结果

ID       requestid         age
1         0001             29
1         0005             29
2         0004             30
3         0004             34
1         0001             27
1         0005             27

我想要下面的结果:

ID       requestid         age
1         0001             29
1         0005             27
2         0004             30
3         0004             34

我知道这很简单,我错过了一些东西。 任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

// Hook after add to cart Added by Atiqur
add_action( 'woocommerce_add_to_cart' , 'repair_woocommerce_2_2_8_session_add_to_cart');

function repair_woocommerce_2_2_8_session_add_to_cart( ){
    if ( defined( 'DOING_AJAX' ) ) {
        wc_setcookie( 'woocommerce_items_in_cart', 1 );
        wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( WC()->cart->get_cart() ) ) );
        do_action( 'woocommerce_set_cart_cookies', true );
    }
}

OR

select ID, requestid, age from table1 t1
inner join table t2
on t2.parentid =t1.id AND t2.requestId = t1.requestId
ORDER BY ID
相关问题