简体   繁体   中英

How to get all rows from a column with WHERE clause and INNER JOIN

i have a query where it searches for a Car word and returns result, but when i search with INNER JOINS a row of Car word is missing. how to get all rows ?

select word.lemma, synset.pos, synset.definition 
from word 
left join sense on word.wordid = sense.wordid 
left join synset on sense.synsetid = synset.synsetid 
where word.lemma = 'car' 
order by synset.pos

Result:

在此输入图像描述

SELECT 
    a.lemma AS `word`,
    c.definition,
    c.pos AS `part of speech`,
    d.sample AS `example sentence`
FROM
    word a
        INNER JOIN
    sense b ON a.wordid = b.wordid
        INNER JOIN
    synset c ON b.synsetid = c.synsetid
        INNER JOIN
    sampletable d ON c.synsetid = d.synsetid
WHERE
    a.lemma = 'car'

Result:

在此输入图像描述

Change all your INNER JOIN so they say LEFT JOIN instead

The row is disappearing because one of your declared relationships is broken, and there is no matching row in b for this particular row in a, or no matching row in c for a particular row in b etc

By using left joins, the database will keep the row from a instead of removing it, and fill in nulls for every column value from b(or c, whichever table it is) where there is no row that matches a

By looking at the NULLs you will be able to see where the missing row is and you can decide to add it. If you do add it then the INNER JOIN will start showing the row again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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