SQL交叉搜索两个表

时间:2011-06-10 19:41:54

标签: mysql sql

我有以下2张表..

mysql> describe catalog_category_reference;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment | 
| name     | text    | NO   |     | NULL    |                | 
| class_id | text    | NO   |     | NULL    |                | 
+----------+---------+------+-----+---------+----------------+

mysql> describe product_import_queue;
+-----------------------+---------+------+-----+---------+----------------+
| Field                 | Type    | Null | Key | Default | Extra          |
+-----------------------+---------+------+-----+---------+----------------+
| id                    | int(11) | NO   | PRI | NULL    | auto_increment | 
| unique_id             | text    | NO   |     | NULL    |                | 
| category_code         | text    | NO   |     | NULL    |                | 
| item_code             | text    | NO   |     | NULL    |                | 
| ffl_flag              | int(11) | NO   |     | NULL    |                | 
| name                  | text    | NO   |     | NULL    |                | 
| price                 | text    | NO   |     | NULL    |                | 
| image                 | text    | NO   |     | NULL    |                | 
| custom_options_flag   | int(11) | NO   |     | NULL    |                | 
| custom_options_string | text    | NO   |     | NULL    |                | 
| short_desc            | text    | NO   |     | NULL    |                | 
| long_desc             | text    | NO   |     | NULL    |                | 
| process_status        | int(11) | NO   |     | 0       |                | 
+-----------------------+---------+------+-----+---------+----------------+

我想搜索product_import_queue并找到catalog_Category_reference中不存在的“category_code”。请注意,category_code存储在class_id下的catalog_reference表中。我可以在一个查询中执行此操作吗?我尝试过类似......

SELECT category_code FROM product_import_queue
LEFT JOIN catalog_category_reference ON product_import_queue.category_code = catalog_category_reference.class_id;

但那不是我想要的,我还没有完全理解JOIN的。

3 个答案:

答案 0 :(得分:5)

你几乎就在那里......只需要添加一个where子句......

SELECT category_code 
FROM   product_import_queue 
       LEFT JOIN catalog_category_reference 
         ON product_import_queue.category_code = 
            catalog_category_reference.class_id 
WHERE  catalog_category_reference.class_id IS NULL  

答案 1 :(得分:1)

SELECT category_code FROM product_import_queue
LEFT JOIN catalog_category_reference ON product_import_queue.category_code = catalog_category_reference.class_id
WHERE catalog_category_reference.class_id is null

答案 2 :(得分:0)

 SELECT category_code FROM product_import_queue LEFT JOIN catalog_category_reference ON product_import_queue.category_code = catalog_category_reference.class_id WHERE catalog_category_reference.class_id IS NULL

此查询将仅查找queque中category_code不属于类别参考

的产品