2017-06-21 70 views
1

我想從具有以下條件的數據庫表「build_wiki_notes_meta」中獲取build_notes,我正在使用下面的查詢,它不給我任何輸出,任何人都可以提供指導如何解決這個問題?有什麼不對?有更好的查詢(更快)?查詢以獲得最新的生成筆記,它不爲空,並且不匹配字符串

1.MAX date_created

2.MATCHES給定software_product_id

3.build_notes不null

4.build_notes不匹配Currently no notes for this build

QUERY:

select 
    meta_name, 
    build_notes 
from 
(
    select meta_name, MAX(date_created) AS date_created, build_notes 
    from build_wiki_notes_meta 
    where software_product_id = 105 
    order by date_created desc 
) as tmp 
where (tmp.build_notes is NOT null) AND 
     (tmp.build_notes NOT LIKE '%Currently no notes for this build%') 

數據庫表: -

enter image description here

回答

0

您當前的查詢有幾個問題,我也想不出一個很好的方式,以詞組你比在下面給出其他查詢。你想根據一系列條件來定位一組特定的記錄。除了這些條件之外,您還希望具有最新創建日期的記錄。請注意,由於擁有最新日期,兩個或更多記錄之間可能會有聯繫。下面的查詢僅使用WHERE子句中的子查詢來檢查最新日期。

SELECT 
    meta_name, 
    build_notes 
FROM build_wiki_notes_meta 
WHERE 
    software_product_id = 105 AND 
    build_notes IS NOT NULL AND 
    build_notes NOT LIKE '%Currently no notes for this build%' AND 
    date_created = (SELECT MAX(date_created) FROM build_wiki_notes_meta 
        WHERE software_product_id = 105 AND 
          build_notes IS NOT NULL AND 
          build_notes NOT LIKE '%Currently no notes for this build%') 

如果你一定會有永遠只能是一個最新的記錄和/或你不介意只是總是取回一個單一的記錄,那麼我們就可以簡化這個以下幾點:

SELECT 
    meta_name, 
    build_notes 
FROM build_wiki_notes_meta 
WHERE 
    software_product_id = 105 AND 
    build_notes IS NOT NULL AND 
    build_notes NOT LIKE '%Currently no notes for this build%' 
ORDER BY date_created DESC 
LIMIT 1 
相關問題