Using a Join to Create a Lookup Table from Descriptive Labels : Simple JOIN « Join « SQL / MySQL






Using a Join to Create a Lookup Table from Descriptive Labels

     
mysql>
mysql> CREATE TABLE coin
    -> (
    ->     id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->     date CHAR(5) NOT NULL, # 4 digits + mint letter
    ->     denom CHAR(20) NOT NULL, # denomination (e.g., Lincoln cent)
    ->     PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> CREATE TABLE denom
    -> (
    ->  denom_id        INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  PRIMARY KEY (denom_id)
    -> )
    -> SELECT DISTINCT denom AS name FROM coin;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> INSERT INTO coin (date,denom) VALUES('1944s','Lincoln cent');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1977','Roosevelt dime');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1955d','Lincoln cent');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1938','Jefferson nickel');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1964','Kennedy half dollar');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1959','Lincoln cent');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1945','Jefferson nickel');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1905','Buffalo nickel');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1924','Mercury head dime');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('2010','Roosevelt dime');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1937','Mercury head dime');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO coin (date,denom) VALUES('1977','Kennedy half dollar');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> INSERT INTO denom (name) SELECT DISTINCT denom FROM coin;
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

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

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

   
    
    
    
    
  








Related examples in the same category

1.Using More Than one Table
2.Self join
3.Simple table join
4.Join three tables
5.Query data from two tables
6.JOIN two tables with alias name
7.Using a Join to Control Query Output Order
8.Return the first names and surnames of both the sales rep and the customer, as well as the value of the sale
9.Query data from two tables 2
10.Finding Rows in One Table That Match Rows in Another
11.Identify records from author table that corresponds to the author name, use its a_id value to find matching re
12.Using information in the book table to find information in the author table
13.Finding Rows with No Match in Another Table
14.Shorten the output column list to include only columns from the author table
15.List each author from the author table, and whether or not you have any books by the author
16.Using table alias to qualify column name when column names exist
17.Using table alias to qualify column name
18.PSEUDONYMS FOR TABLE NAMES
19.Qualify the column name with table name