Jump to content

Whats wrong with this SQL query..


billy_111

Recommended Posts

Hi,

 

I'm trying to use the following delete query:

 

DELETE FROM People LEFT JOIN PeopleCon ON People.Pid = PeopleCon.Person_id WHERE People.Pname = 'Peter Kay','Jake Welsh' 

 

Reason for doing it like this is i want to delete from 2 different tables which can be joined by an ID, table structure is below:

 

CREATE TABLE IF NOT EXISTS `People` (

`Pid` int(11) NOT NULL AUTO_INCREMENT,

`Pname` varchar(255) NOT NULL,

`Pdateadded` datetime NOT NULL,

`Pdeleted` int(1) NOT NULL,

PRIMARY KEY (`Pid`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

 

 

CREATE TABLE IF NOT EXISTS `PeopleCon` (

`PCid` int(5) NOT NULL AUTO_INCREMENT,

`Person_id` int(5) NOT NULL,

`PCHid` int(5) NOT NULL,

`Pid` int(5) NOT NULL,

`PCorder` int(2) NOT NULL,

`PCdateadded` datetime NOT NULL,

`PCdeleted` int(1) NOT NULL,

PRIMARY KEY (`PCid`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

 

I even tried doing it like this after a quick google search:

 

DELETE t1, t2 FROM People AS t1 LEFT JOIN PeopleCon AS t2 ON t1.Pid = t2.Person_id WHERE t1.Pname = 'Peter Kay','Jake Welsh'; 

 

But had no luck.

 

Any ideas?

 

Thanks

Link to comment
Share on other sites

Are you trying to delete from both tables or just one? Regardless, this isn't proper:

 

WHERE t1.Pname = 'Peter Kay','Jake Welsh'

 

It needs to be:

 

WHERE t1.Pname IN('Peter Kay','Jake Welsh')

 

or:

 

WHERE t1.Pname = 'Peter Kay' OR t1.Pname =  'Jake Welsh'

 

Link to comment
Share on other sites

If the two table have a clear relationship as in your case, I rather prefer to implement/enforce the Referential Integrity at DataBase level with a FOREIGN KEY and an ON DELETE CASCADE. In that way you only be deleting from the "Master" table PEOPLE... the ON DELETE CASCADE will take care of automatically delete from the PeopleCon Table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.