2012-07-11 76 views

回答

2

Is this what you had in mind?

select 
    d.id_destination, v.country, d.name 
from 
    destination d 
    inner join destination_visa dv on dv.id_destination = d.id_destination 
    inner join visa v on dv.id_visa = v.id_visa 
order by nullif (v.country, (select v2.country 
          from destination_visa dv2 
          inner join visa v2 
          on dv2.id_visa = v2.id_visa 
          where dv2.id_destination = 6)); 

还有一个子查询,它可能被预选到变量中。

我不认为子查询应该提出一个问题,因为它不与外部查询相关,这意味着它应该只执行一次。至于排序,空值先排序,所以我用它来取消给定目的地的国家匹配。或者你可以用它来使打算更清楚:

order by case when v.country = (select v2.country 
           from destination_visa dv2 
           inner join visa v2 
           on dv2.id_visa = v2.id_visa 
           where dv2.id_destination = 6) 
       then 0 -- Move matching country to front 
       else 1 -- Not a matching country 
     end, 
     v.country 
+0

是的!你能解释一下订单吗?另外,你可以谈谈使用子查询这种方式的复杂性吗? – brainydexter 2012-07-11 12:44:50

+0

@brainydexter请再次检查答案。 – 2012-07-11 12:51:18

+0

完美!感谢你的解释,另一个例子帮助我理解它。虽然我有一个与'nullif'相关的问题。如果条件为真,则nullif返回null。但是,'ORDER BY TRUE'(或者'false',对于这个问题)呢? – brainydexter 2012-07-11 12:54:20

相关问题