如何用两个表写sql命令?

时间:2012-09-17 09:16:09

标签: mysql sql

table **salesrule_coupon**: fileds as the following:

           coupon_id   code

             1        Registered_send_5   
             2         test

表:salesrule_coupon_usage文件如下:

    coupon_id  customer_id   times_used

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

现在,我想选择times_used其中customer {id = $ id的code =Registered_send_5。如何编写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服务器版本对应的手册,以获得正确的语法,在'salesrule_coupon_usage附近使用左边的连接salesrule_coupon作为b在a.co'第1行

3 个答案:

答案 0 :(得分:5)

为什么你有'的{​​{1}}进入?

应该是$id而不是FROM

答案 1 :(得分: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;
                                                           ^

答案 2 :(得分: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;
                                                          ^