A表:
pagerId name standardPrice
0 广州 1
1 南方 1
3 羊城 1
B表:
id userId pagerId actualPrice
0 1 0 0.8
1 1 1 0.8 如:
查询userId为1的客户,我要的查询结果集是:
pagerId name standardPrice
0 广州 0.8
1 南方 0.8
3 羊城 1其实上面的结果就是如果B表有该用户记录,就将B表该用户的standardPrice字段值替换A表的standardPrice字段值
如果B表没有该用户记录,就直接取A表的standardPrice字段值
pagerId name standardPrice
0 广州 1
1 南方 1
3 羊城 1
B表:
id userId pagerId actualPrice
0 1 0 0.8
1 1 1 0.8 如:
查询userId为1的客户,我要的查询结果集是:
pagerId name standardPrice
0 广州 0.8
1 南方 0.8
3 羊城 1其实上面的结果就是如果B表有该用户记录,就将B表该用户的standardPrice字段值替换A表的standardPrice字段值
如果B表没有该用户记录,就直接取A表的standardPrice字段值
from A表 t1 left join B表 t2 on t1.pagerId=t2.pagerId where t2.userId=1
from A t1 left join B t2 on t1.pagerId=t2.pagerId where t2.userId=1
from A表 a left join B表
on a.pageid = b.pageid
where userid = 1
如果B表没有该用户记录,就直接取A表的standardPrice字段值
SELECT A.pagerId,A.name,IFNULL(B.actualPrice,A.standardPrice) standardPrice FROM A left join B on A.pagerId=B.pagerId
左连接就已经把A表的记录全部查出,而B表作为辅助,查B.actualPrice的意思是把B.actualPrice查出不存在的为空,用ifnull就把空值拿A.standardPrice做替换。
select a.pageid,a.name,ifnull(b.actualprice,a.standardprice)standardPrice from a left join b on a.pagerid=b.pagerid