加入三个mysql表 - 内连接

时间:2014-11-07 07:33:20

标签: php mysql inner-join

我有三张桌子:

第一张表:

牵引:

1   id  
2   price   
3   date    
4   user_id 
5   ip  
6   code    
7   succ    
8   admin_seen  
9   user_seen   
10  coupon_id
11  cradit_start_date
12  cradit_end_date
13  shop_id

第二张表:

优惠券:

1   id  
2   title   
3   slider_title    
4   short_title 
5   pic 
6   text    
7   attrs   
8   jest    
9   terms_of_use    
10  how_to_use

第三张表:

商店:

1   id
2   title   
3   address 
4   phone

现在,我想根据id tractions获取这些值:

tractions.price ,tractions.cradit_start_date,tractions.cradit_end_date,
    shops.address,coupons.terms_of_use,coupons.attrs

我编写了这个函数,但它返回null值:

function getContentACopounForQRReader($id){

    mysql_query("SET CHARACTER SET utf8");
    $result_qr  =   mysql_query("select tractions.price ,tractions.cradit_start_date,tractions.cradit_end_date,
    shops.address,coupons.terms_of_use,coupons.attrs
    from tractions
    INNER JOIN shops ON shops.id = tractions.shop_id
    INNER JOIN coupons ON coupons.id = tractions.coupon_id
    where tractions.id = $id");

    $row = mysql_fetch_object($result_qr);

    $marr = array('price'=>$row->price);

    return $marr ;

}

(我知道我必须使用mysqli或PDO)

0 个答案:

没有答案