如何使用mysql
在SubQuery结果中设置默认 VALUESELECT
p.`id`, p.`name`, p.`class_name`, cpd.`status_team`,
cpd.`home`, cpd.`guest`, cpd.`mvp`, cpd.`oscar`,
cpd.`wam`, cpd.`status`, cpd.`added_date`,
(SELECT result FROM result_cards WHERE `id` = cpd.`result`) AS DEFAULT(`result`)
FROM `cron_players_data` cpd
INNER JOIN `players` p ON cpd.`player_id` = p.id
WHERE cpd.`added_date` = '2012-03-29' AND cpd.team_id = '15'
当我删除此 DEFAULT()时,Query将正常执行。实际上我想默认结果值为0或者帮助肯定是值得赞赏的
答案 0 :(得分:5)
解决方案IFNULL
SELECT
p.`id`,p.`name`,p.`class_name`,cpd.`status_team`,cpd.`home`,cpd.`guest`,cpd.`mvp`,
cpd.`oscar`,cpd.`wam`,cpd.`status`,cpd.`added_date` ,
IFNULL((SELECT result FROM result_cards WHERE `id` = cpd.`result`),0) AS `result`
FROM `cron_players_data` cpd INNER JOIN `players` p ON cpd.`player_id` = p.id
WHERE cpd.`added_date` = '2012-03-29' AND cpd.team_id = '15'
答案 1 :(得分:1)
您应该将子查询移动到连接。但是我无法理解你在尝试用DEFAULT()做什么。您需要解释您想要实现的目标。
SELECT
p.`id`, p.`name`, p.`class_name`, cpd.`status_team`,
cpd.`home`, cpd.`guest`, cpd.`mvp`, cpd.`oscar`,
cpd.`wam`, cpd.`status`, cpd.`added_date`,
IFNULL(rc.`result`, 0) AS `result`
FROM `cron_players_data` cpd
INNER JOIN `players` p
ON cpd.`player_id` = p.id
LEFT JOIN result_cards rc
ON cpd.`result` = rc.id
WHERE cpd.`added_date` = '2012-03-29'
AND cpd.team_id = '15'