日期:2014-05-18  浏览次数:20496 次

求一条简单的sql语句??
有三张表
T1(modelID,ModelName,packageId,fiorder) packageId是T3表里面的packageId,但在此表不一定有值
T2(uuid,modelID,content) modelID是T1表里面的modelID,一定有值
T3(packageId,packageName,fiorder) fiorder为顺序号

我现在要查出
modelID,modelName,packageName,content

packageName,content没有值则显示空,要求按T3表的fiorder从小到大排序,再按T1表的fiorder从小到大排序;如果T1表的packageID为空,则把这些记录排在最前面??

------解决方案--------------------
SQL code
select modelID,modelName,packageName,content from 
(
select t2.modelID,t1.modelName,t3.packageName,t2.content,t1.packageID,t3.fiorder t3_fiorder, t1.fiorder t1_fiorder
from t3 left join t1 on t3.packageId = t1.packageId
left join t2 on t1.modelID t2.modelID
) t 
order by (case when packageID is null then 1 else 2 end) , t3_fiorder, t1_fiorder