SQL перекрестный поиск по двум таблицам - PullRequest
1 голос
/ 10 июня 2011

У меня есть следующие 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 хранится в таблице catalog_reference под class_id. Могу ли я сделать это одним запросом? Я пытался что-то вроде ...

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 ]

5 голосов
/ 10 июня 2011

Вы были почти там ... просто нужно добавить предложение 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 голос
/ 10 июня 2011
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
0 голосов
/ 10 июня 2011
 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

В этом запросе будут найдены только те товары в очереди, которых код категории не входит в ссылку категории

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...