How to write sql command with two tables?

table **salesrule_coupon**: fileds as the following: coupon_id code 1 Registered_send_5 2 test 

table: salesrule_coupon_usage written as follows:

  coupon_id customer_id times_used 1 1 1 1 ... 14 ... 1.. 

Now, I want to select times_used , where code =Registered_send_5 , where customer_id = $ id. how to write sql command? thanks u.

The following is mine, but it does not work.

 $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."; 

when i put

  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 

in phpmyadmin. He shows

You have an error in the SQL syntax; check the manual that matches your MySQL server version for the correct syntax to use next to 'salesrule_coupon_usage as left join salesrule_coupon as b on a.co' on line 1

+4
source share
3 answers

there is an apostrophe (single quote) that should not be there, and you mistakenly wrote "OT":

 $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
source

Why do you have ' from $id ?

It must be FROM , not FORM

+5
source

There is an error in your request at the end:

 $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
source

Source: https://habr.com/ru/post/1434561/


All Articles