例如有2张表:
表person如下:
id name identity state
0 zhang 0 0
1 yang 1 0
2 wang 0 1
3 li 1 1表bm如下
lbid bm sm
01 0 男
01 1 女
02 0 健康
02 1 亚健康
02 2 死亡如果通过左连接查询,要得到如下结果:
id name sex state
0 zhang 男 健康
1 yang 女 健康
2 wang 男 亚健康
3 li 女 亚健康我的左连接语句如下:
select id, name, hw_sex.sm, hw_state.sm
from ((person as p
left join bm as hw_sex on( p.identity = hw_sex.bm and hw_identity.lbid = "01"))
left join bm as hw_state on(p.state = hw_state.bm and hw_identity.lbid = "02"))但是运行怎么老报错呢?如何解决,大家帮忙看看
表person如下:
id name identity state
0 zhang 0 0
1 yang 1 0
2 wang 0 1
3 li 1 1表bm如下
lbid bm sm
01 0 男
01 1 女
02 0 健康
02 1 亚健康
02 2 死亡如果通过左连接查询,要得到如下结果:
id name sex state
0 zhang 男 健康
1 yang 女 健康
2 wang 男 亚健康
3 li 女 亚健康我的左连接语句如下:
select id, name, hw_sex.sm, hw_state.sm
from ((person as p
left join bm as hw_sex on( p.identity = hw_sex.bm and hw_identity.lbid = "01"))
left join bm as hw_state on(p.state = hw_state.bm and hw_identity.lbid = "02"))但是运行怎么老报错呢?如何解决,大家帮忙看看
改为如下即可
select * from (
select id,
name,
hw_sex.sm sex_sm,
hw_state.sm state_sm
from ((person as p
left join bm as hw_sex on( p.identity = hw_sex.bm and hw_identity.lbid = "01"))
left join bm as hw_state on(p.state = hw_state.bm and hw_state.lbid = "02"))
)
where ....
p.name,
hw_sex.sm sex_sm,
hw_state.sm state_sm
from person as p
left join bm as hw_sex on p.identity = hw_sex.bm
left join bm as hw_state on p.state = hw_state.bm
where hw_identity.lbid = "01" and hw_state.lbid = "02"