Jump to content

Selection problem


Revelation

Recommended Posts

Hi,

 

I've got this (simplified) table:

 

Name Value
Ben 1
Ben 2
Tom 3

Hank4

 

What I would like to do is select every name from this table that has not got 2. The problem is that I can't use a subquery (MySQL 4.0) and that WHERE NOT Value = '2' doensn't work, because then Ben gets selected anyway (via Ben 1) and that's not what I want.

 

Can someone help me?

 

Thanks!

Link to comment
Share on other sites

SELECT name, COUNT(*) as ct

FROM table

GROUP BY name

HAVING ct = 1

 

Interpreting your question as eliminating any name that has a value of 2, rather than a count of 2, use a temporary table instead of a subquery

 

CREATE TEMPORARY TABLE tmp SELECT name FROM tablename WHERE value = 2

 

Then

 

SELECT t.name

FROM tablename t

LEFT JOIN tmp p ON t.name = p.name

WHERE p.name IS NULL;

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.