views:

60

answers:

2

Here is my query:

$query="Delete b
Where Exists 
    (
        Select 1
        From a
        Where a.poster_password = '$pass'
        And a.ad_id = '$id'
        And a.classified_id = b.classified_id
    )
Delete a
Where a.poster_password = '$pass'
And a.ad_id = '$id'";

I get this error:

"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 'Where Exists ( Select 1 From a Where a.poster_p' at line 2"

If you need more input let me know...

Whats wrong here?

Thanks

UDPATE:

Just a Q: Do I need to specify also that a = "this table" and b = "another table" or does MySql get that by this code?

As for the new code posted where to use FROM and a terminator semicolon, wont work and give this error:

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 'Delete FROM a Where a.poster_password = 'xxxxxxxxxxxxxxxxxxxxx' at line 10

UPDATE2:

 $query="Delete FROM $sql_table
Where Exists 
(
Select 1
From classified
Where classified.poster_password = '$pass'
And classified.ad_id = '$id'
And classified.classified_id = $sql_table.classified_id
);
Delete FROM classified
Where classified.poster_password = '$pass'
And classified.ad_id = '$id'";

And when I echo $query: (fordon is in this case $sql_table variable.)

Delete FROM fordon Where Exists ( Select 1 From classified Where classified.poster_password = 'xxxxx' And classified.ad_id = 'motorbat_166250627' And classified.classified_id = fordon.classified_id ); Delete FROM classified Where classified.poster_password = 'xxxxx' And classified.ad_id = 'motorbat_166250627'

Thanks again

A: 

The two deletes need to be separate statements ( and executed separately ).

James Anderson
MySQL supposts multi-table deletes: http://dev.mysql.com/doc/refman/5.0/en/delete.html. However the OP's syntax appears incorrect. There should be a `FROM` in there.
Daniel Vassallo
+2  A: 

You're not specifying the tables to delete from. Try:

$query="Delete FROM b
Where Exists 
    (
        Select 1
        From a
        Where a.poster_password = '$pass'
        And a.ad_id = '$id'
        And a.classified_id = b.classified_id
    );
Delete FROM a
Where a.poster_password = '$pass'
And a.ad_id = '$id'";

I've also added in a semicolon after the end of the first DELETE query. If you want to run both at the same time, you'll need a separator to terminate the first query, before you run the second version.

Re. your question edit about MySQL "getting" the tables - if a and b are aliases here, then no, MySQL doesn't know what a and b are. You'll need to alias the tables, or replace a and b with the actual table names.

richsage
Check my update please, this wont work either for some reason
Camran
Okay, changed the aliases, but still get the same error in the update of my Q... hmm
Camran
Can you post your exact query string now?
richsage
yes, check update again pls...
Camran