SQL cross searching two tables
I have the following 2 tables..
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 | |
+----------+---------+------+-----+---------+----------------+
and
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 | 开发者_JAVA技巧 |
| short_desc | text | NO | | NULL | |
| long_desc | text | NO | | NULL | |
| process_status | int(11) | NO | | 0 | |
+-----------------------+---------+------+-----+---------+----------------+
I want to search through product_import_queue and find the "category_code"'s that do not exist in the catalog_Category_reference. Please note that, the category_code
is stored in the catalog_reference table under class_id
. Can I do this in one query? I've attempted something like...
SELECT category_code FROM product_import_queue
LEFT JOIN catalog_category_reference ON product_import_queue.category_code = catalog_category_reference.class_id;
But thats not what I'm looking for and I don't fully understand JOIN's yet.
You were almost there...just need to add a where clause...
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
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
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
This query will find only products in queque which category_code isn't in category reference
精彩评论