tryso 发表于 2006-4-20 22:28:50

超难的MySQL问题

如果table1的某一行的a 与table2的c对应,就删除table1的这一行。

table1
a   | b
-----
1   | 100
2   | 200

table2
c   |
----
1   |

在SQL server 里面可以这样写到:
delete table1 from table1 a join table2 b on a.a=b.c;

但在MySQL里面就不行,应该怎么改?谢谢!

涩兔子 发表于 2006-4-20 23:54:56


DELETE
    FROM table1 t1 LEFT JOIN table2 t2 ON t1.a = t2.c
    WHERE t1.a = xxx

tryso 发表于 2006-4-21 09:07:02

出现问题:

mysql> DELETE
    ->   FROM table1 t1 LEFT JOIN table2 t2 ON t1.a = t2.c ;
ERROR 1064 (42000): 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 'LEFT
JOIN table2 t2 ON t1.a = t2.c' at line 2
mysql>

涩兔子 发表于 2006-4-21 12:42:07

http://www.google.com/url?sa=t&ct=res&cd=1&url=http%3A%2F%2Fbugs.mysql.com%2Fbug.php%3Fid%3D2171&ei=MmFIRIDkOoiUogLut_2iBQ&sig2=mgf7pnaBw7jTXvnxh9Abcw
MySQL居然不支持连接删除
http://dev.mysql.com/doc/refman/5.0/en/delete.html
DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;

Or:

DELETE FROM t1, t2 USING t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;

推荐使用PostgreSQL了 :mrgreen:

tryso 发表于 2006-4-21 19:25:13

网上有这样回复,不知道运行效率怎样?

delete from t1 where t1.a in (select t2.c from t2);

虚拟主机一般只支持 MySQL或 SQL Server。无奈~

jiangtao9999 发表于 2006-4-21 20:08:46

MySQL 不支持查询嵌套,所以 SQL 语句要构思严谨……………

好像 5 支持嵌套了?

玛宁 发表于 2006-4-29 14:31:51

MySQL用InnoDB做Table的Engine的话就持连接删除了
页: [1]
查看完整版本: 超难的MySQL问题