Creating a temporary table to hold the maximum price, and then joining it with the other tables: : Join Select « Join « SQL / MySQL






Creating a temporary table to hold the maximum price, and then joining it with the other tables:

    
mysql>
mysql> CREATE TABLE author
    -> (
    ->     a_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # author ID
    ->     name VARCHAR(30) NOT NULL, # author name
    ->     PRIMARY KEY (a_id),
    ->     UNIQUE (name)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE book
    -> (
    ->     a_id INT UNSIGNED NOT NULL, # author ID
    ->     p_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # book ID
    ->     title VARCHAR(100) NOT NULL, # title of book
    ->     state VARCHAR(2) NOT NULL, # state where purchased
    ->     price INT UNSIGNED, # purchase price (dollars)
    ->     INDEX (a_id),
    ->     PRIMARY KEY (p_id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO author (name) VALUES
    ->  ('Tom'),
    ->  ('Monet'),
    ->  ('Jack'),
    ->  ('Picasso'),
    ->  ('Mary')
    -> ;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'Database', 'IN', 34 FROM author WHERE name = 'Tom';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'SQL', 'MI', 87 FROM author WHERE name = 'Tom';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'MySQL', 'KY', 48 FROM author WHERE name = 'Jack';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'XML', 'KY', 67    FROM author WHERE name = 'Jack';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'Java', 'IA', 33   FROM author WHERE name = 'Jack';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'HTML', 'NE', 64   FROM author WHERE name = 'Mary';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> CREATE TABLE tmp SELECT MAX(price) AS max_price FROM book;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT author.name, book.title, book.price
    -> FROM author, book, tmp
    -> WHERE book.price = tmp.max_price
    -> AND book.a_id = author.a_id;
+------+-------+-------+
| name | title | price |
+------+-------+-------+
| Tom  | SQL   |    87 |
+------+-------+-------+
1 row in set (0.00 sec)

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

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

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

   
    
    
    
  








Related examples in the same category

1.Simple Join two tables
2.JOINs Across Two Tables
3.JOINs Across Two Tables with link id
4.JOINs Across Three or More Tables
5.Table joins and where clause
6.Select distinct column value during table join
7.Select distinct column values in table join
8.Count joined table
9.Performing a Join Between Tables in Different Databases
10.LINESTRING type column: One or more linear segments joining two points; one-dimensional.
11.Select other columns from rows containing a minimum or maximum value is to use a join.
12.Retrieve the overall summary into another table, then join that with the original table:
13.Tests a different column in the book table to find the initial set of records to be joined with the author tab
14.Select the maximum population value into a temporary table, Then join the temporary table to the original one
15.To display the authors by name rather than ID, join the book table to the author table
16.To display the author names, join the result with the author table
17.The summary is written to a temporary table, which then is joined to the cat_mailing table to produce the reco
18.Addition of a WHERE clause for table join
19.Join more than two tables together.
20.Creating Straight Joins: STRAIGHT_JOIN
21.Use the basic join syntax and you specify the STRAIGHT_JOIN table option in the SELECT clause
22.Creating Natural Joins
23.Joining Columns with CONCAT
24.A basic join
25.Rewriting Sub-selects as Joins
26.Display game code, name, price and vendor name for each game in the two joined tables
27.Get the player number, the sex, and the name of each player who joined the club after 1980.
28.Join two tables with char type columns
29.Convert subqueries to JOINs
30.Join with another database
31.Join two tables with shared columns values
32.Qualify column name with table name during the table join
33.Three tables join together
34.Compare date type value during table join
35.Join on syntax
36.Natural join syntax
37.Join with Integer type column
38.Join three table together