开发者

MySQL complex SQL query showing empty values where a table-column doesn't match

开发者 https://www.devze.com 2023-03-13 04:49 出处:网络
I have two tables in my database: en_page and mwpage. Both have a page_title column. What I want is to select the data where both of the page_title values match.

I have two tables in my database: en_page and mwpage. Both have a page_title column. What I want is to select the data where both of the page_title values match.

For example

SELECT EN.page_title ENP, MW.page_title MWP
FROM en_page AS EN, mwpage AS MW
WHERE EN.page_title =  'Main_Page'
AND MW.page_t开发者_如何学运维itle =  'Main_Page'
LIMIT 0 , 30

This query works fine and returns following data:

ENP |       MWP
Main_Page|  Main_Page

But I have an exceptional case. If any of the table.page_title values don't match the condition, the query returns no result set. I want it to still return a result set where the table which didn't match should be empty, like the following:

ENP |       MWP
Main_Page|  

I don't know how to do this. Can anyone help?


Try this

SELECT EN.page_title ENP, MW.page_title MWP
FROM 
    en_page AS EN
    LEFT OUTER JOIN mwpage AS MW ON EN.page_title = MW.page_title
WHERE EN.page_title =  'Main_Page'
LIMIT 0 , 30


SELECT EN.page_title ENP, MW.page_title MWP
FROM en_page AS EN, mwpage AS MW
WHERE (EN.page_title =  'Main_Page' OR EN.page_title='')
AND (MW.page_title =  'Main_Page' OR MW.page_title = '')    
LIMIT 0 , 30


To handle the NULL case, just use a CASE WHEN

SELECT EN.page_title ENP, CASE WHEN  MW.page_title IS NULL THEN '' ELSE  MW.page_title END MWP
FROM 
    en_page AS EN
    LEFT OUTER JOIN mwpage AS MW ON EN.page_title = MW.page_title
WHERE EN.page_title =  'Main_Page'
LIMIT 0 , 30
0

精彩评论

暂无评论...
验证码 换一张
取 消