Finding Rows with No Match in Another Table : Simple JOIN « Join « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Join » Simple JOIN 
Finding Rows with No Match in Another Table
        
mysql>
mysql> CREATE TABLE author
    -> (
    ->     a_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # author ID
    ->     name VARCHAR(30NOT NULL, # author name
    ->     PRIMARY KEY (a_id),
    ->     UNIQUE (name)
    -> );
Query OK, rows affected (0.01 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(100NOT NULL, # title of book
    ->     state VARCHAR(2NOT NULL, # state where purchased
    ->     price INT UNSIGNED, # purchase price (dollars)
    ->     INDEX (a_id),
    ->     PRIMARY KEY (p_id)
    -> );
Query OK, rows affected (0.00 sec)

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

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

mysql>
mysql>
mysql>
mysql> SELECT FROM author ORDER BY a_id;
+------+---------+
| a_id | name    |
+------+---------+
|    | Tom     |
|    | Monet   |
|    | Jack    |
|    | Picasso |
|    | Mary    |
+------+---------+
rows in set (0.00 sec)

mysql> SELECT FROM book ORDER BY a_id, p_id;
+------+------+----------+-------+-------+
| a_id | p_id | title    | state | price |
+------+------+----------+-------+-------+
|    |    | Database | IN    |    34 |
|    |    | SQL      | MI    |    87 |
|    |    | MySQL    | KY    |    48 |
|    |    | XML      | KY    |    67 |
|    |    | Java     | IA    |    33 |
|    |    HTML     | NE    |    64 |
+------+------+----------+-------+-------+
rows in set (0.00 sec)

mysql>
mysql> SELECT FROM author, book WHERE author.a_id != book.a_id;
+------+---------+------+------+----------+-------+-------+
| a_id | name    | a_id | p_id | title    | state | price |
+------+---------+------+------+----------+-------+-------+
|    | Monet   |    |    | Database | IN    |    34 |
|    | Jack    |    |    | Database | IN    |    34 |
|    | Picasso |    |    | Database | IN    |    34 |
|    | Mary    |    |    | Database | IN    |    34 |
|    | Monet   |    |    | SQL      | MI    |    87 |
|    | Jack    |    |    | SQL      | MI    |    87 |
|    | Picasso |    |    | SQL      | MI    |    87 |
|    | Mary    |    |    | SQL      | MI    |    87 |
|    | Tom     |    |    | MySQL    | KY    |    48 |
|    | Monet   |    |    | MySQL    | KY    |    48 |
|    | Picasso |    |    | MySQL    | KY    |    48 |
|    | Mary    |    |    | MySQL    | KY    |    48 |
|    | Tom     |    |    | XML      | KY    |    67 |
|    | Monet   |    |    | XML      | KY    |    67 |
|    | Picasso |    |    | XML      | KY    |    67 |
|    | Mary    |    |    | XML      | KY    |    67 |
|    | Tom     |    |    | Java     | IA    |    33 |
|    | Monet   |    |    | Java     | IA    |    33 |
|    | Picasso |    |    | Java     | IA    |    33 |
|    | Mary    |    |    | Java     | IA    |    33 |
|    | Tom     |    |    HTML     | NE    |    64 |
|    | Monet   |    |    HTML     | NE    |    64 |
|    | Jack    |    |    HTML     | NE    |    64 |
|    | Picasso |    |    HTML     | NE    |    64 |
+------+---------+------+------+----------+-------+-------+
24 rows in set (0.00 sec)

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

mysql> drop table author;
Query OK, 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.Using a Join to Create a Lookup Table from Descriptive Labels
9.Return the first names and surnames of both the sales rep and the customer, as well as the value of the sale
10.Query data from two tables 2
11.Finding Rows in One Table That Match Rows in Another
12.Identify records from author table that corresponds to the author name, use its a_id value to find matching re
13.Using information in the book table to find information in the author 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.