2017-10-20 147 views
-1

我想從我的數據庫表中選擇數值生日。 如果它不爲空,則輸出應該是日期。但是如果它是空的,那麼輸出應該是空的。如何從mysql數據庫獲取日期,如果它不爲空?

'SELECT *, 
    case when isnull(mydate) then null else date_format(mydate, cats.bithday,"%d.%m.%y") end as birthday; 
    FROM cats 
    LEFT JOIN dogs ON cats.dogs=dogs.id' 

我得到一個錯誤:

Syntax error or access violation: 1582 Incorrect parameter count in the call to native function 'date_format'

+1

參見:爲什麼要我爲了什麼,在我看來是一個非常簡單的SQL查詢提供MCVE?](HTTPS ://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry

回答

2
SELECT *, ifnull(cats.bithday,'') as cats_birthday 
FROM cats 
LEFT JOIN dogs ON cats.dogs=dogs.id 

結果:

case (birthday is null) 
    cats_birthday = '' 
case (birthday is not null) 
    cats_birthday = db-value 
+0

所以這意味着我這樣寫:'ifnull(DATE_FORMAT(cats.birthday,「%d。%m。%y」),'')AS cats_birthday' – Jarla

+1

@Jarla是的,你是對的 –

0

你給DATE_FORMAT三個參數,但它僅需要兩個。

MySQL docs here

DATE_FORMAT(date,format)

相關問題