Display game code, name, price and vendor name for each game in the two joined tables : Join Select « Join « SQL / MySQL






Display game code, name, price and vendor name for each game in the two joined tables

    
mysql>
mysql>
mysql> CREATE TABLE IF NOT EXISTS games
    -> (
    ->   id             VARCHAR(10)     PRIMARY KEY,
    ->   vendor INT             NOT NULL,
    ->   name           CHAR(20)        NOT NULL,
    ->   price          DECIMAL(6,2)    NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> # insert 5 records into the "games" table
mysql> INSERT INTO games (id, vendor, name, price)   VALUES ("371/2209", 1, "Scrabble", 14.50);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO games (id, vendor, name, price)   VALUES ("373/2296", 2, "Jenga", 6.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO games (id, vendor, name, price)   VALUES ("360/9659", 1, "Uno", 11.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO games (id, vendor, name, price)   VALUES ("373/5372", 3, "Connect", 5.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO games (id, vendor, name, price)   VALUES ("370/9470", 3, "Bingo", 8.99);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> # create a table called "vendors"
mysql> CREATE TABLE IF NOT EXISTS vendors
    -> (
    ->   id             INT     PRIMARY KEY,
    ->   name           CHAR(20)        NOT NULL,
    ->   location       CHAR(20)        NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> # insert 3 records into the "vendors" table
mysql> INSERT INTO vendors (id, name, location)   VALUES (1, "Mattel Inc", "El Segundo, Ca, USA");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO vendors (id, name, location)   VALUES (2, "Hasbro Inc", "Pawtucket, RI, USA");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO vendors (id, name, location)   VALUES (3, "J.W.Spear Plc", "Enfield, Middx, UK");
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT   games.id        AS ProductCode,
    ->  games.name      AS Game,
    ->  vendors.name    AS Vendor,
    ->  games.price     AS Price
    -> FROM     games, vendors
    -> WHERE    vendors.id = games.vendor;
+-------------+----------+---------------+-------+
| ProductCode | Game     | Vendor        | Price |
+-------------+----------+---------------+-------+
| 371/2209    | Scrabble | Mattel Inc    | 14.50 |
| 373/2296    | Jenga    | Hasbro Inc    |  6.99 |
| 360/9659    | Uno      | Mattel Inc    | 11.99 |
| 373/5372    | Connect  | J.W.Spear Plc |  5.99 |
| 370/9470    | Bingo    | J.W.Spear Plc |  8.99 |
+-------------+----------+---------------+-------+
5 rows in set (0.00 sec)

mysql>
mysql> # delete these sample tables
mysql> DROP TABLE games;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE vendors;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
  








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.Creating a temporary table to hold the maximum price, and then joining it with the other tables:
16.To display the authors by name rather than ID, join the book table to the author table
17.To display the author names, join the result with the author table
18.The summary is written to a temporary table, which then is joined to the cat_mailing table to produce the reco
19.Addition of a WHERE clause for table join
20.Join more than two tables together.
21.Creating Straight Joins: STRAIGHT_JOIN
22.Use the basic join syntax and you specify the STRAIGHT_JOIN table option in the SELECT clause
23.Creating Natural Joins
24.Joining Columns with CONCAT
25.A basic join
26.Rewriting Sub-selects as Joins
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