Use a two-stage approach involving one query that selects the maximum size into a SQL variable, and another th : Variable « Select Clause « SQL / MySQL






Use a two-stage approach involving one query that selects the maximum size into a SQL variable, and another th

       
at refers to the variable in its WHERE clause
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> 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 @max := MAX(pop) FROM states;
+------------------+
| @max := MAX(pop) |
+------------------+
|         29760021 |
+------------------+
1 row in set (0.00 sec)

mysql> SELECT @max AS 'highest population', name FROM states WHERE pop = @max;
+--------------------+------------+
| highest population | name       |
+--------------------+------------+
|           29760021 | California |
+--------------------+------------+
1 row in set (0.00 sec)

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

   
    
    
    
    
    
    
  








Related examples in the same category

1.Variables can be assigned values from arbitrary expressions.
2.Using variable the calculate the new column ID
3.Global System Variables versus System Variables at the Connection Level
4.Using SQL Variables in Queries
5.Returns all global variables and their settings
6.Return the session system variables
7.If you're retrieving the value of a global variable, you must also precede the variable name with the global keyword
8.If you want to retrieve a value for a session variable, rather than a global variable
9.Set the query_cache_limit variable to 1000000 at the global level
10.To set a session-level variable
11.Check the state of the variables with the following command:
12.Ordinary variables indicated by a prefixed @ sign.
13.System and server variables: Such variables contain states or attributes of the MySQL server.
14.Variable Assignment
15.Use of Variables
16.SQL variables hold single values.
17.If the variable has not been used previously, that value is NULL:
18.To set a variable explicitly to a particular value, use a SET statement.
19.Variable names are case sensitive:
20.SQL variables may be used to store the results of intermediate calculations.
21.Calculate the overall average and save it in a variable, then compare each driver's average to the saved value
22.To use a SQL variable, store the highest price in it, then use the variable to identify the record containing
23.To find a value and save it in a variable
24.SELECT statement defines two variables
25.User Variables
26.Set a variable specifically.
27.Create the user variable PLAYERNO and initialize it with the value 7.
28.Can the result of a SELECT statement be assigned to a user variable
29.Compare decimal type variables
30.Create and set MySQL variable
31.Using concat to create value for variable
32.Using variable in where clause
33.Assin constant value to a variable
34.Assign the result from a sql to a variable
35.Assign a constant to a variable
36.Assign and create three variables inside one select statement
37.Assign the result of an aggregate function to a variable
38.Using variable to pass value between sql statement
39.Compare to a variable
40.Insert statement with variable
41.Assign value to a variable with :=
42.Using select ... into to assign value to variabls
43.A two-query approach
44.Advancing the rank only when values change
45.Use the differential as follows to produce team standings that include winning percentage and GB values
46.To calculate a standard deviation based on n-1 degrees of freedom instead
47.Calculating Linear Regressions or Correlation Coefficients
48.Assigning Ranks
49.Comparing a Table to Itself