views:

120

answers:

6

I have a MySQL table and I want to extract all rows except if the column contains a specific value. Is this possible?

My table structure

+----+------+------+------+
| ID | col1 | col2 | col3 |
+----+------+------+------+
|  1 | blah | blah | boo  |
+----+------+------+------+
|  2 | blah | blah | blah |
+----+------+------+------+
|  3 | blah | blah | boo  |
+----+------+------+------+

So if col3 contains boo, don't extract that:

+----+------+------+------+
| ID | col1 | col2 | col3 |
+----+------+------+------+
|  2 | blah | blah | blah |
+----+------+------+------+
A: 

The following excludes col3 if it equals 'boo'.

select * from mytable where col3 <> 'boo'

Other than that, I don't know what you're looking for.

Eric
That's not what I mean. Due to the way my table is structured this would not be possible.
Ben Shelock
@Ben: please post an example of your table structure to add some lubrication to the discussion.
Adam Bernier
@Ben: Yes, please explain. I don't see why this wouldn't work.
musicfreak
+3  A: 

It's certainly possible. The following code should do the trick

SELECT * FROM `table` WHERE `column` != "boo"

If you're looking where more than one column, than add the following afterwords:

&& `column` != "boo"

for every column you need.

Assuming, of course, that you mean that you DON'T want boo. If you want only boo, then take away the exclamation point.

waiwai933
SleepyCod
This is my query however I get an error. Can you spot the problem?SELECT * FROM `links` WHERE `page` ='$id' AND WHERE `season` !='*' ORDER BY `season` ASC, `episode` ASC
Ben Shelock
Take out the second Where. That should be your problem.
waiwai933
+1  A: 

If by "contains" you mean "is equal to":

SELECT * FROM table WHERE column <> 'VALUE TO EXCLUDE'

Or if by "contains" you literally mean "contains":

SELECT * FROM table WHERE column NOT LIKE '%VALUE TO EXCLUDE%'

EDIT -- To answer your updated question:

SELECT * FROM table WHERE col3 <> 'boo'
James Skidmore
A: 

select * from MyTable where col3 <> 'boo'

RedFilter
A: 

You can use regular expressions in your where clause if your cloumn data and requirement are not simple.

e.g.

SELECT * FROM foo WHERE bar REGEXP '[[:alnum:]]+';

or

SELECT * FROM foo WHERE bar NOT REGEXP '[[:alnum:]]+';
Jonathan Fingland
+1  A: 
SELECT * FROM YourTable WHERE col3 != 'boo';
soulmerge