wc1
id name
1  南昌
2  武汉
3  北平
wc2
id name
1  广州
2  成都
myGo,viaF表示wc1下的所有城市均可用。
id startF viaF price
1  上海   wc1  12
2  上海   wc2  14
3  上海   南昌 8
查询时,可以得到这样吗?1 上海 南昌 12
2 上海 武汉 12
3 上海 北平 12
4 上海 广州 14
5 上海 成都 14
6 上海 南昌 8
是我的表设计有问题,还是一条语句不能实现。

解决方案 »

  1.   

    我感觉设计的确是不太好,这样不好关联吧,除非你的表名以及个数确定,可以吧,wc1,wc2合并成一个,并且添加一列,里面放viaf,这样很好得到结果。
      

  2.   


    SELECT a.id,a.startF,b.name,a.price from myGo a ,wc1 b where a.viaF='wc1'
    union all
    SELECT a.id,a.startF,b.name,a.price from myGo a ,wc2 b where a.viaF='wc2'
    union all
    select a.* from myGo a where a.viaF not in('wc2','wc1')
      

  3.   

    select a.Name from T_C_CONSUMELEVELS as a,T_C_CUSTOMERLEVELS as b where b.SumLevel between a.LowerLimit and a.UpperLimit and CustomerID=120914120222248 and CalcType=1;
    select a.Name from T_C_CONSUMELEVELS as a, T_C_CUSTOMERLEVELS as b where b.CountLevel between a.LowerLimit and a.UpperLimit and b.CustomerID=120914120222248 and a.CalcType=2;这两条语句的结果分别是‘铜’和‘石’ 我这么样能把他们拼起来呢
      

  4.   

    select a.id,a.startF ,COALESCE(b.viaF,a.viaF) ,a.price
    from myGo a left join (
    select 'wc1' as viaF, name from wc1
    union all
    select 'wc2' as viaF, name from wc2
    ) b on a.viaF=b.viaF
      

  5.   

    多谢谢,COALESCE函数,我第一遇到。谢谢了。
    coalesce()解释:返回参数中的第一个非空表达式(从左向右);
    显示第一个不是NULL的值。
    多个参数也一样。mysql> select coalesce(1,2,3,4) ;
    +-------------------+
    | coalesce(1,2,3,4) |
    +-------------------+
    |                 1 | 
    +-------------------+
    1 row in set (0.00 sec)mysql> select coalesce(null,2,3,4) ;
    +----------------------+
    | coalesce(null,2,3,4) |
    +----------------------+
    |                    2 | 
    +----------------------+
    1 row in set (0.00 sec)mysql> select coalesce(null,null,3,4) ;
    +-------------------------+
    | coalesce(null,null,3,4) |
    +-------------------------+
    |                       3 | 
    +-------------------------+
    1 row in set (0.00 sec)