0

I've got an error saying that" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM table_A INNER JOIN table_B ON table_A.name = table_B.name' at line 1 "

The sql query is :

$sql="UPDATE table_A SET table_A.quantity = table_A.quantity - table_B.quantity FROM table_A INNER JOIN table_B ON table_A.name = table_B.name WHERE table_B.status = 'APPROVED'"; 

Please help me out of this error. Thank you.

5
  • INNER should be inner join? Commented Mar 10, 2017 at 19:57
  • You wrote INNER instead of INNER JOIN Commented Mar 10, 2017 at 20:00
  • uppsss sorry. it's a typo. I'm using INNER JOIN Commented Mar 10, 2017 at 20:00
  • You can't do an UPDATE like that in MySQL - see this question for the correct syntax: stackoverflow.com/questions/1262786/… Commented Mar 10, 2017 at 20:00
  • Wait, what... I don't think that's legal in MySql Commented Mar 10, 2017 at 20:02

2 Answers 2

0

"INNER" should be "INNER JOIN"

I think you also have to take out "table_A" from "SET quantity"

 $sql="UPDATE table_A SET quantity = table_A.quantity - table_B.quantity FROM table_A INNER JOIN table_B ON table_A.name = table_B.name WHERE table_B.status = 'APPROVED'"; 
Sign up to request clarification or add additional context in comments.

1 Comment

but, I've got this : Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in line $count=mysql_num_rows($sql);
0

You have syntax error.

Correct syntaxs for UPDATE with INNER JOIN:

UPDATE T1,T2 [INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1 SET T1.C2 = T2.C2, T2.C3 = expr WHERE condition 

Query:

$sql="UPDATE table_A INNER JOIN table_B ON table_A.name = table_B.name SET table_A.quantity = table_A.quantity - table_B.quantity WHERE table_B.status = 'APPROVED'"; 

2 Comments

but, I've got this : Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in line $count=mysql_num_rows($sql);
You must use mysql_affected_rows().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.