简体   繁体   English

MySQL LEFT JOIN 未返回所需结果

[英]MySQL LEFT JOIN not returning the desired results

I have what I think is a simple MySQL issue that I cannot seem to find an answer that applies to what I am doing.我有一个我认为是简单的 MySQL 问题,我似乎找不到适用于我正在做的事情的答案。 I have two tables with a LEFT JOIN.我有两个带有 LEFT JOIN 的表。 For this example, table 1 will only have 1 row and table 2 will have 3 rows.对于此示例,表 1 将只有 1 行,表 2 将有 3 行。

TABLE 1: inv_info表 1:inv_info

id  manufacturer    model   description     quantity
1   SP              A55     USB disk        7

TABLE 2: inv_category表 2:inv_category

id  categories
1   USB
1   Hard disk
1   SSD

Here is the query:这是查询:

SELECT manufacturer, model, description, quantity, categories 
FROM inv_info 
LEFT JOIN inv_category ON inv_info.id=inv_category.id 
WHERE manufacturer LIKE '%usb%' 
   OR model LIKE '%usb%' 
   OR description LIKE '%usb%' 
   OR categories LIKE '%usb%'

If my search item is one of the category names, it returns results for each category item, not just the one I searched for.如果我的搜索项是类别名称之一,它会返回每个类别项的结果,而不仅仅是我搜索的那个。 I want it to search all the category rows for this search term, but not return a row for each row in inv_categories.我希望它搜索此搜索词的所有类别行,但不为 inv_categories 中的每一行返回一行。

The results I get from the above query:我从上述查询中得到的结果:

manufacturer    model   description     quantity    categories
SP              A55     USB disk        7           SATA
SP              A55     USB disk        7           SSD
SP              A55     USB disk        7           usb

I want it to search both tables, but only return 1 row being data from table 1, not a row of data for each category in table 2.我希望它搜索两个表,但只返回表 1 中的 1 行数据,而不是表 2 中每个类别的数据行。

Desired result:期望的结果:

manufacturer    model   description quantity    categories
SP              A55     USB disk    7           usb
SELECT manufacturer, model, description, quantity, categories 
FROM inv_info 
LEFT JOIN inv_category ON inv_info.id=inv_category.id 
WHERE (    manufacturer LIKE '%usb%' 
        OR model LIKE '%usb%' 
        OR description LIKE '%usb%' )
  AND categories LIKE '%usb%'

UPDATE更新

This query text implicitly converts LEFT JOIN to INNER JOIN due to the condition by the column from inv_category table in WHERE clause.由于 WHERE 子句中inv_category表中的列的条件,此查询文本将 LEFT JOIN 隐式转换为 INNER JOIN。

So correct query text is所以正确的查询文本是

SELECT manufacturer, model, description, quantity, categories 
FROM inv_info 
LEFT JOIN inv_category ON inv_info.id=inv_category.id AND categories LIKE '%usb%'
WHERE (    manufacturer LIKE '%usb%' 
        OR model LIKE '%usb%' 
        OR description LIKE '%usb%' )  

or maybe even甚至可能

SELECT manufacturer, model, description, quantity, categories 
FROM ( SELECT *
       FROM inv_info 
       WHERE (    manufacturer LIKE '%usb%' 
               OR model LIKE '%usb%' 
               OR description LIKE '%usb%' ) ) models
LEFT JOIN ( SELECT *
            FROM inv_category 
            WHERE categories LIKE '%usb%' ) categories USING (id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM