Add an expression to the WHERE clause that explicitly excludes the reference : Where « Where Clause « SQL / MySQL






Add an expression to the WHERE clause that explicitly excludes the reference

       
mysql>
mysql> CREATE TABLE states
    -> (
    ->  name            VARCHAR(30) NOT NULL,   # state name
    ->  abbrev          CHAR(2) NOT NULL,               # 2-char abbreviation
    ->  statehood       DATE,                                   # date of entry into the Union
    ->  pop                     BIGINT,                                 # population as of 4/1990
    ->  PRIMARY KEY (abbrev)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> insert into states(name, abbrev, statehood, pop)values
    -> ("Alabama","AL","1819-12-14",4040587),
    -> ("Alaska","AK","1959-01-03",550043),
    -> ("Arizona","AZ","1912-02-14",3665228),
    -> ("Arkansas","AR","1836-6-15",2350725),
    -> ("California","CA","1850-9-9",29760021),
    -> ("Colorado","CO","1876-8-1",3294394),
    -> ("Connecticut","CT","1788-1-9",3287116),
    -> ("Delaware","DE","1787-12-7",666168),
    -> ("Florida","FL","1845-3-3",12937926),
    -> ("Georgia","GA","1788-1-2",6478216),
    -> ("Hawaii","HI","1959-08-21",1108229),
    -> ("Idaho","ID","1890-7-3",1006749),
    -> ("Illinois","IL","1818-12-3",11430602),
    -> ("Indiana","IN","1816-12-11",5544159),
    -> ("Iowa","IA","1846-12-28",2776755),
    -> ("Kansas","KS","1861-1-29",2477574),
    -> ("Kentucky","KY","1792-6-1",3685296),
    -> ("Louisiana","LA","1812-4-30",4219973),
    -> ("Maine","ME","1820-3-15",1227928),
    -> ("Maryland","MD","1788-4-28",4781468),
    -> ("Massachusetts","MA","1788-2-6",6016425),
    -> ("Michigan","MI","1837-1-26",9295297),
    -> ("Minnesota","MN","1858-5-11",4375099);
Query OK, 23 rows affected (0.00 sec)
Records: 23  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT s2.name, s2.statehood
    -> FROM states AS s1, states AS s2
    -> WHERE s1.name = 'New York' AND s1.name != s2.name
    -> AND YEAR(s1.statehood) = YEAR(s2.statehood)
    -> ORDER BY s2.name;
Empty set (0.00 sec)

mysql>
mysql> drop table states;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Not equal in where
2.Getting the List of Products That Are on Catalog Promotion
3.Using Where Conditions
4.WHERE Clause Comparisons
5.Combining WHERE Conditions
6.Calculation in WHERE clause
7.Compare and calculate in Where clause
8.Do Calculation in Where and order
9.Where clause: nested conditions
10.Where clause: calculation and equal condition
11.Where clause: compare
12.Where clause: XOR
13.Use where clause the narrow down results
14.Use CURRENT_DATE in where clause
15.WHERE TRUE OR FALSE
16.Change localhost to the name of the machine where you'll be working.
17.WHERE clauses can test multiple conditions.
18.To put first those records where people sent messages to themselves
19.Using a WHERE clause that matches up values in the author ID column
20.Add a WHERE clause that looks for NULL values in the book column that is named in the ON clause
21.Adds an additional condition to the WHERE clause and the calculated columns must have a total greater than 20
22.A WHERE clause that contains two expressions (conditions)
23.Searches for surnames that have an e anywhere in the name and then end with an e:
24.Show all records where the color is "Silver" and the price is above 100.00
25.Show records where make is "Krups", "Gaggia" or "DeLonghi" and the model is not "TSK-182" or "EC410"
26.Show all records where the color is not "Silver" or the price is below 100.00
27.Get the number of items for each color where the price exceeds 150.00 and when there is more than 1 item for t
28.Check day name in where clause
29.Compare the results from two statements