我目前使用下面的查詢來連接兩個表一起簡化聯合查詢:SQL -
select distinct book_id, clean_page_content as page_content, 'clean' as status, reg_date
from book_db_clean_pages t
where book_id = 'sarafi_book_5'
and reg_date = '2015-08-26'
UNION all
select distinct book_id, dirty_page_content as page_content, 'dirty' as status, reg_date
from book_db_dirty_pages e
where book_id = 'sarafi_book_5'
and reg_date = '2015-08-26'
然而,當我想尋找一個不同的book_id我需要改變兩個分句(一個在上面,一個低於UNION)。同樣的原則適用於reg_date。所以我想知道是否有可能簡化我的查詢只是使用可能的命令,如加入或其他事情。原則上,我要尋找一個解決方案像這樣:
select distinct t.book_id, (t.clean_page_content or e.dirty_page_content) as page_content,
('clean' or 'dirty') as status, t.reg_date
from book_db_clean_pages t, book_db_dirty_pages e
where t.book_id = e.book_id and t.reg_date = e.reg_date
and t.book_id = 'sarafi_book_5'
and t.reg_date = '2015-08-26'
無論如何UNION,無論如何刪除所有副本都不需要做SELECT DISTINCT。 – jarlh
你有什麼看法並從中選擇? –