tags:

views:

117

answers:

7

I have a table that has about 6 fields. I want to write a SQL statement that will return all records that do not have "England" in the country field, English in the language field & english in the comments field.

What would the sql query be like?

A: 

Sounds like home work to me

Jay
+1  A: 

Start with this and modify as necessary:

SELECT *
FROM SixFieldTable
WHERE Country <> 'England'
AND language <> 'english'
AND comments NOT LIKE '%english%'

Hope this helps.

Mike DeFehr
Thanks. I didnt know if I should use AND keyword or OR keywordWhat would I use the OR keyword for?
Perhaps a grounding in Boolean logic would prove helpful.http://en.wikipedia.org/wiki/Boolean_logic
Duncan Beevers
Unfortunately your requirements in the comments were: (must not contain "England" in the country field; must not contain "english" in the language field; must not contain "english" in the comments field). What about uppercase "English"? Mike's answer would only work if you wanted to match EXACTLY England in Country and english in Language. What if the Country's value was North England?
p.campbell
OR is probably not indicated here, but basically it is for when you have two (or more) conditions, but you want to see the record if either (any) condition is true - with AND, both (all) conditions must be true for the record to be returned
Mike DeFehr
Hence the prefix to "start with this and modify" - the intention is to get started with some syntax that starts to provide results - not provide a definitive answer (I hope you realize that) - yes, a grounding in boolean logic would not be a bad idea
Mike DeFehr
zombat's answer points out more clearly that you can have exact inequality (<> 'England') for example, 'North England' <> 'England' or "not contains" inequality (NOT LIKE '%England%') - 'North England' *is* LIKE '%England%' - you can also have "starts with" (LIKE 'England%') and "ends with" (LIKE '%England'). Confused yet?
Mike DeFehr
+1  A: 

Are you wanting something like

select * from myTableOfMadness
where country <> 'England'
and language <> 'English'
and comments not like '%english%'

Not sure if you want 'and's or 'or's, or all 'not' comparisons. Your sentence structure is somewhat misleading.

glasnt
+3  A: 

Well, your question depends a lot on what DBMS you're using and what your table set up looks like. This would be one way to do it in MySQL or TSQL:

SELECT *
FROM tbl
WHERE country NOT LIKE '%England%' AND language NOT LIKE '%english%'
     AND comments NOT LIKE '%english%';

The way you word your question makes it sound like all these fields could contain a lot of text, in which case the above query would be the way to go. However, more likely than not you'd be looking for exact matches in a real database:

SELECT *
FROM tbl
WHERE country!='England' AND language!='english'
    AND comments NOT LIKE '%english%';
zombat
zombat, you probably want to edit your second example, I doubt you meant language!='%english%' (accidentally included %'s?)
Amber
@Dav, % is for matching any character before and after 'english'. so yes, it is necessary.
Michael
@Dav - yeah, good catch. I saw it just before I saw your comment. Thanks. :)
zombat
A: 

Try This

Select * From table
Where Country Not Like '%England%'
 And Language Not Like '%English%'
 And comments Not Like '%English%'
Charles Bretana
+1  A: 

The above solutions do not appear to account for possible nulls in the columns. The likes of

Where country <> 'England'

will erroneously exclude entries where Country is null, under default SQL Server connection settings.

Instead, you could try using

IsNull(Country, '') <> 'England'
CodeByMoonlight
+1  A: 

To ignore case:

SELECT *
FROM SixFieldTable
WHERE LOWER(Country) <> 'england' AND
LOWER(language) <> 'english' AND
LOWER(comments) NOT LIKE '%english%'
Sarah Vessels