As no one else has stepped up I'll offer a few comments.
Code:
delete from table_a where x_id in (select x_id from table_b where field_a = 2345)
First comment: I avoid SQL Server same as I avoid anything from the source, so cannot offer help specific to the platform. An initial quick test indicated to me that MySQL behaved differently, but after paying better attention I am suitably horrified to find that is not the case
when table_a has a column of same name - yikes!
I suppose the relevant logic is that described by MySQL as
correlated subqueries, which permits references to tables in the outer query from within the subquery. That combined with implicit qualification of field names results in this behavior.
BUT:
Quote:
Originally Posted by SoftSprocket
In the query in question table_a did not have a column named field_a so SQL Server must not only have satisfied the requirement for x_id in the subquery but also ignored the where clause
|
MySQL definitely fails with an
Unknown column in where clause in this case. I am surprised at that behavior even for a micros-chism! That fails the implicit qualification case described so that is a pretty serious error.
Second comment: My own habit is to explicitly qualify
table.column in all but the simplest queries, and to avoid subqueries wherever possible as it is almost always possible|better to do it with a join. For the example you posted my own approach would have been something like...
Code:
DELETE table_a FROM table_b JOIN table_a USING(x_id) where table_b.field_a=2345
That said, I have no idea whether that platform supports
multi-table delete, but if so this would have kept you out of that particular spot of trouble.