![]() The above explanation was pretty convoluted, so let's throw in a photo of food to break up the text: Thus, two exclusive filters joined by an OR can result in nothing being excluded. Thus, 'John' rows are included, even though they fail the other (optional) requirement: name is not equal to 'John'.īut again, OR makes the requirements optional. The condition as stated above includes a row if its name is not equal to 'Daniel'. Well, a row in which the name is 'John' is also a row in which the name is not 'Daniel'. Guess what that is equal to? SELECT * FROM baby_namesīy using OR, the WHERE condition can be described in English as: Include all the rows in which the name Let's revisit the previous example that used != SELECT * FROM baby_names WHERE name != 'Daniel' OR name != 'John' OR wants everyone to have fun! But if you are trying to exclude rows, then OR becomes a bit too permissive. ![]() For example the following query: SELECT * FROM baby_names WHERE name = 'Daniel' OR name = 'John'Ĭontains all of the rows by these two separate queries: SELECT * FROM baby_names WHERE name = 'Daniel' SELECT * FROM baby_names WHERE name = 'John' Remember that the OR keyword, by its very definition, expands the possible set of rows that can pass a condition. Here's a query for rows in which the name is neither 'Daniel' nor 'John', for all years except 2013: SELECT * FROM baby_names WHERE name != 'Daniel' AND name != 'John' AND year != 2013 Beware of using OR in an exclusionary condition The following query will return all rows in which the name is not 'Daniel': SELECT * FROM baby_names WHERE name != 'Daniel'Īs with the plain equals-sign operator, we can use != in a variety of combinations. To filter for non-matches, or rather, not equals, put an exclamation mark before the equals sign. We know how to filter rows by using the equals sign to make exact matches. I cover a battery of examples and variations, but for 90% of your actual work, you can easily get by with the most simple kinds of statements. They're useful and essential in SQL, and everywhere else, too. The good news is that these logical operations are universal to math and programming. There's no way to get past that perception until you practice it a little and slowly realize that there's not many actual new things to memorize, it's just that – as in plain English – there's many different ways to describe things as true or false – and you will be clumsy at it, at first. ![]() If you're new to programming, the bad news is that this will seem like a wall of new syntax and minutiae. If that's something you've done in another programming language, then you can basically skip this chapter. Instead, it covers the more common ways to express logical comparisons. Word of warning: This tutorial has very little to do with SQL specifically.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |