子查询的表引用on和where
查询结构是
select (select XXX from tableA left join tableC on tableA.report_id = tableC.report_id where tableA.id = (SELECT max(id) FROM tableA where national_id = ni.national_id)) as new_field
from tableB as ni
这样是写是ok的
但是如果把where条件拿到on里面去
select (select XXX from tableA left join tableC on tableA.report_id = tableC.report_id AND tableA.id = (SELECT max(id) FROM tableA where national_id = ni.national_id)) as new_field
from tableB as ni
就会报错,无法识别ni.national_id,我猜测on里面识别的表别名,只能是on条件之前的,而不能是后面的
select (select XXX from tableA left join tableC on tableA.report_id = tableC.report_id where tableA.id = (SELECT max(id) FROM tableA where national_id = ni.national_id)) as new_field
from tableB as ni
这样是写是ok的
但是如果把where条件拿到on里面去
select (select XXX from tableA left join tableC on tableA.report_id = tableC.report_id AND tableA.id = (SELECT max(id) FROM tableA where national_id = ni.national_id)) as new_field
from tableB as ni
就会报错,无法识别ni.national_id,我猜测on里面识别的表别名,只能是on条件之前的,而不能是后面的
留言
張貼留言