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字段值

解决方案 »

  1.   

    select t1.pagerId,  t1.name,  ifnull(t2.actualPrice,t1.standardPrice) as standardPrice
      from A表 t1 left join B表 t2 on t1.pagerId=t2.pagerId where t2.userId=1
      

  2.   

    select t1.pagerId,  t1.name,  T1.standardPrice*ifnull(t2.actualPrice,1) as standardPrice
      from A t1 left join B t2 on t1.pagerId=t2.pagerId where t2.userId=1
      

  3.   

    select pagerId  name  IFNUL(b.standardPrice,a.standardPrice) as standardPrice
    from A表 a left join B表
    on a.pageid = b.pageid
    where userid = 1
      

  4.   

    2、3、4楼都是用if null 跟 left join上面几楼的还不符合要求。用if null是可以但我要的是A表记录全部显示,并且如果B表有该用户记录,就将B表该用户的standardPrice字段值替换A表的standardPrice字段值 
    如果B表没有该用户记录,就直接取A表的standardPrice字段值
      

  5.   


    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做替换。
      

  6.   

    楼上正解。。
    select a.pageid,a.name,ifnull(b.actualprice,a.standardprice)standardPrice from a left join b on a.pagerid=b.pagerid