2012-09-17 50 views
4
table **salesrule_coupon**: fileds as the following: 

      coupon_id code 

      1  Registered_send_5 
      2   test 

表:salesrule_coupon_usage的Fileds如下所示:如何用兩個表編寫sql命令?

coupon_id customer_id times_used 

    1    1   1 
    1 
    ...    14 ...  1.. 

現在,我要選擇出times_used其中code =Registered_send_5其中CUSTOMER_ID = $ ID。如何編寫sql命令?謝謝你。

以下是我的,但它不能正常工作。

$sql = "SELECT times_used FORM salesrule_coupon_usage as a 
     left join salesrule_coupon as b on a.coupon_id = b.coupon_id 
     where b.code = 'Registered_send_5' and customer_id=".'$id."; 

,當我把

SELECT times_used FORM salesrule_coupon_usage as a 
      left join salesrule_coupon as b on a.coupon_id = b.coupon_id 
      where b.code = 'Registered_send_5' and customer_id=1 

在phpMyAdmin。它顯示

您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,在第1行使用'salesrule_coupon_usage作爲左加入salesrule_coupon作爲b上的a.co'附近的正確語法。

+0

爲什麼它不工作?你有什麼錯誤?請發帖 –

+0

不校對自己的代碼是一個壞主意。 –

回答

4

有一個撇號(單引號)在那裏,你拼錯「FROM」:

$sql = "SELECT times_used FORM salesrule_coupon_usage as a 
         ^
    left join salesrule_coupon as b on a.coupon_id = b.coupon_id 
    where b.code = 'Registered_send_5' and customer_id= ".'$id; 
                 ^
4

有在年底在查詢時出現錯誤:

$sql = "SELECT times_used FORM salesrule_coupon_usage as a 
    left join salesrule_coupon as b on a.coupon_id = b.coupon_id 
    where b.code = 'Registered_send_5' and customer_id= ".$id; 
                 ^
+0

但是當我給一個值的id,它也顯示一個錯誤 – downa1234

5

爲什麼你從$id'

應該FROM而不是FORM