0

这是我的简化查询:

SELECT `student`.`id`, `student`.`firstname`, `student`.`lastname`,
IFNULL(`review`.`score`, '0') AS `adminreview_score`,
`reviewcolor`.`color` AS adminreview_color FROM (`student`)
LEFT JOIN `review` ON `review`.`student_id` = `student`.`id` AND
review.reviewtype_id = 13
LEFT JOIN `reviewcolor` ON `reviewcolor`.`score` = `adminreview_score`
WHERE `student`.`id` > '0'

这是我得到的错误:

Error Number: 1054

Unknown column 'adminreview_score' in 'on clause'

请注意,表中可能没有review以下情况:

`review`.`student_id` = `student`.`id` AND review.reviewtype_id = 13

在这种情况下,我想adminreview_score被设置为0,我希望reviewcolor.colorbeNULL或 empry ( )

谢谢

4

2 回答 2

1

试试这个:(我用实际的表达式替换了别名。请注意,SELECT 子句中的别名不能用于 SQL 表达式的其余部分。)

SELECT `student`.`id`, `student`.`firstname`, `student`.`lastname`,
IFNULL(`review`.`score`, '0') AS `adminreview_score`,
`reviewcolor`.`color` AS adminreview_color 
FROM (`student`)
    LEFT JOIN `review` ON `review`.`student_id` = `student`.`id` AND review.reviewtype_id = 13
    LEFT JOIN `reviewcolor` ON `reviewcolor`.`score` = IFNULL(`review`.`score`, '0')
WHERE `student`.`id` > '0'
于 2012-07-02T19:49:13.913 回答
1

你可以使用一个变量,试试这个

set @a:='';

SELECT `student`.`id`, `student`.`firstname`, `student`.`lastname`,
IFNULL(@a:=`review`.`score`, @a:=0),
`reviewcolor`.`color` AS adminreview_color FROM (`student`)
LEFT JOIN `review` ON `review`.`student_id` = `student`.`id` AND
review.reviewtype_id = 13
LEFT JOIN `reviewcolor` ON `reviewcolor`.`score` = @a
WHERE `student`.`id` > '0'
于 2012-07-02T19:54:06.547 回答