Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-24-2011, 12:52 AM   PM User | #1
rfresh
Regular Coder

 
Join Date: Jun 2007
Location: Los Angeles
Posts: 545
Thanks: 81
Thanked 5 Times in 5 Posts
rfresh is an unknown quantity at this point
Delete Help Needed

I have the following SELECT query statement which works fine. What I want to do now is turn it into a DELETE statement. But when I replace SELECT with DELETE, I get an error. I've tried removing the DISTINCT but I still can't get it to delete.

Thanks for any help.

PHP Code:
SELECT DISTINCT E.*, COUNT(ED.id) AS dcnt FROM Employees E LEFT JOIN EmpDepts ED ON E.id ED.emp_id WHERE E.company_index 78 AND E.employee 0 GROUP BY E.last_name HAVING dcnt 
__________________
RalphF
Business Text Messaging Services
https://www.MobileTextingService.com
rfresh is offline   Reply With Quote
Old 07-24-2011, 03:12 AM   PM User | #2
vinyl-junkie
$object->toCD-R(LP);


 
vinyl-junkie's Avatar
 
Join Date: Jun 2003
Posts: 3,054
Thanks: 2
Thanked 22 Times in 22 Posts
vinyl-junkie is on a distinguished road
I believe you can do something like this:

Code:
DELETE FROM EMPLOYEES WHERE Employees.id IN
(
SELECT DISTINCT E.*, COUNT(ED.id) AS dcnt 
FROM Employees E 
LEFT JOIN EmpDepts ED ON E.id = ED.emp_id 
WHERE E.company_index = 78 
AND E.employee = 0 
GROUP BY E.last_name HAVING dcnt = 0  
)
__________________
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
SNAP to it!
vinyl-junkie is online now   Reply With Quote
Users who have thanked vinyl-junkie for this post:
rfresh (07-24-2011)
Old 07-25-2011, 02:08 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I don't think so.

When you do ".... WHERE somefield IN ( SELECT xxx FROM yyy ) ...", the SELECT must return *ONLY* ONE FIELD. To wit, the field that matches the somefield field value.

So, *PROBABLY* more like
Code:
DELETE FROM Employees 
WHERE id NOT IN ( SELECT emp_id FROM EmpDepts )
AND company_index = 78 
AND employee = 0
No?
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:18 PM.


Advertisement
Log in to turn off these ads.