2016-09-16 4 views
1
UPDATE newsreactions 
SET newsreactions.enabled = '0' 
FROM newsreactions 
INNER JOIN users ON newsreactions.memberId = users.id 
WHERE users.active = '0' AND users.comment LIKE '%spam%' 

은 어떤 이유로 나는 구문 오류를 받고 있어요 여기서SQL 업데이트에 가입하고

1064 - 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 newsreactions INNER JOIN users ON newsreactions.memberId = users.id WHERE u' at line 3

하지만 그것을 알아낼 수 없습니다. updatesetselect으로 대체하면 올바르게 작동합니다.

+3

태그 사용중인 데이터베이스 질문. –

+0

MySQL 다중 테이블 UPDATE 문에 대한 구문은 다음에서 볼 수 있습니다. http://dev.mysql.com/doc/refman/5.6/en/update.html – spencer7593

답변

2

오류 1064는 MySQL 구문 오류입니다. 올바른 MySQL의 구문은 다음과 같습니다

UPDATE newsreactions nr INNER JOIN 
     users u 
     ON nr.memberId = u.id 
    SET nr.enabled = 0 
WHERE u.active = 0 AND u.comment LIKE '%spam%'; 

참고 : JOINUPDATE 절에 간다

  • .
  • 테이블 별칭을 사용하면 쿼리를 더 쉽게 쓰고 읽을 수 있습니다.
  • 나는 enabledactive이 실제로 숫자 값이라고 추측합니다. 그렇다면 작은 따옴표를 사용하지 마십시오.
+0

Thx! 이 질문에 대한 답변을 사용 : http://stackoverflow.com/questions/9588423/sql-server-inner-join-when-updating 내 질문은 어떻게 다른가요? –

+1

@PascalClaes : 그 다른 질문은 MySQL이 아닌 Microsoft SQL Server에 관한 것입니다. 그것들은 다른 DBMS이며, SQL 문법에는 몇 가지 중요한 차이점이 있습니다. 다중 테이블 UPDATE 구문은 이러한 차이 중 하나의 예입니다. 그것은 당신이 사용하고있는 데이터베이스를 확인하는 것이 중요하다는 큰 이유입니다. – spencer7593

1

join 절은 set 절 앞에 와야한다,와 MySQL에는 from 조항이 없어야합니다 :

UPDATE newsreactions 
JOIN users ON newsreactions.memberId = users.id 
SET newsreactions.enabled = '0' 
WHERE users.active = '0' AND users.comment LIKE '%spam%'