2014-03-26 53 views
1

當我執行以下查詢: -Mysql的設置默認值,如果該項不存在

SELECT users.id, users.username, users.last_login, 
profile_pictures.pic_resized, profile_pictures.pic_blurred, 
user_profile.date_of_birth, user_profile.u_country, 
matches.total_score AS score, 
favorites.id AS is_favorite, 
relationships.id AS is_match, 
profile_visitors.id AS is_profile_viewer, 
block_chat.id AS is_blocked 
FROM users 
INNER JOIN user_profile ON users.id = user_profile.user_id 
LEFT JOIN profile_pictures ON users.id = profile_pictures.user_id 
LEFT JOIN match_collections ON users.id = match_collections.user_id AND match_collections.status='valid' 
LEFT JOIN matches ON matches.collection_id = match_collections.id AND match_id = 44700 
LEFT JOIN favorites ON favorites.subject_id = 11354 AND favorites.object_id = 44700 
LEFT JOIN relationships ON relationships.user_id = 11354 AND relationships.friend_id = 44700 
LEFT JOIN profile_visitors ON profile_visitors.viewed_id = 11354 AND profile_visitors.viewer_id = 44700 
LEFT JOIN block_chat ON block_chat.subject_id = 11354 AND block_chat.object_id = 44700 
WHERE users.id = 11354 

結果顯示:enter image description here 其中is_favorite和is_match爲null這是指有用戶44700沒有條目在那些表格中。

現在的問題是:如何設置一個默認值,如-1而不是NULL

回答

3

您可以使用此: -

SELECT users.id, users.username, users.last_login, 
profile_pictures.pic_resized, profile_pictures.pic_blurred, 
user_profile.date_of_birth, user_profile.u_country, 
matches.total_score AS score, 
if(isnull(favorites.id),-1,favorites.id) AS is_favorite, 
if(isnull(relationships.id),-1,relationships.id) AS is_match, 
profile_visitors.id AS is_profile_viewer, 
block_chat.id AS is_blocked 
FROM users 
INNER JOIN user_profile ON users.id = user_profile.user_id 
LEFT JOIN profile_pictures ON users.id = profile_pictures.user_id 
LEFT JOIN match_collections ON users.id = match_collections.user_id AND match_collections.status='valid' 
LEFT JOIN matches ON matches.collection_id = match_collections.id AND match_id = 44700 
LEFT JOIN favorites ON favorites.subject_id = 11354 AND favorites.object_id = 44700 
LEFT JOIN relationships ON relationships.user_id = 11354 AND relationships.friend_id = 44700 
LEFT JOIN profile_visitors ON profile_visitors.viewed_id = 11354 AND profile_visitors.viewer_id = 44700 
LEFT JOIN block_chat ON block_chat.subject_id = 11354 AND block_chat.object_id = 44700 
WHERE users.id = 11354 

可以使用if(isnull(exp1),exp2,exp3)無論你需要解決nulls

+0

順便說一下,它不是'isnull'而是'ifnull'。 MSSQL中存在'isnull'。 – Rahul

+0

isnull()不能正常工作? – KrazzyNefarious

0

IFNULL應該做的工作:..., IFNULL(favorites.id,-1) as is_favorite, ...

+0

IFNULL不工作,is_null因爲「BhupeshC」的回答很好 – Moussawi7

+0

@ Moussawi7:很奇怪;在我看來,它應該起作用......無論如何,這並不重要,因爲您已經獲得了適合您的解決方案。 – a1ex07

+0

此外,它不能。如果'IFNULL'不適合他,那麼它根本不是MySQL。 – Rahul

1

您可以使用COALESCEIFNULL在mysql中避免這種情況,就像

COALESCE(favorites.id,-1) AS is_favorite 

OR

IFNULL(favorites.id,-1) AS is_favorite 
3

如果你想返回NULL以外的東西,如果這就是在野外使用CASE語句來此改變返回什麼。

case when favorites.id is NULL then '-1' else favorites.id end as is_favorite 
+0

你不處理案件時,它不是空的。 – KrazzyNefarious

+0

你說得對。 如果favorites.id爲NULL,則爲'-1'else else favorites.id以is_favorite結尾。 糾正會做到這一點。 – durbnpoisn

+0

編輯你的答案,以避免downvotes – KrazzyNefarious