开发者

SQL: Order By dictated by a different table

开发者 https://www.devze.com 2023-02-09 00:14 出处:网络
Hopefully very simple SQL question - I\'m just b开发者_开发知识库lacking out :) I have a table of vendors (id, name, description, url). It used to be the web service returned them all sorted by id. Af

Hopefully very simple SQL question - I'm just b开发者_开发知识库lacking out :)

I have a table of vendors (id, name, description, url). It used to be the web service returned them all sorted by id. After a while, I was asked to return sorted by name. Now they want me to allow them to change the order manually - to showcase new vendors.

Suppose I create another table, VendorOrder with (vendorid, placement), what can I put in the Order By section of the original query, to return the vendors sorted by placement?

As always, thanks in advance.

Guy


select 
  vendor.id,
  vendor.name,
  vendor.description,
  vendor.url
from 
  vendors,
  vendorOrder
where
  vendors.id = vendorOrder.vendorId
order by 
  vendorOrder.placement;

Make sure that you find exactly one vendorId in vendorOrder for each id in vendor, otherwise use a left join between the tables.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号