我正在编写使用数据库的应用程序,它从数据库中选择一些数据并在列表视图中显示给用户。从数据库中选择项目时,我必须保留一些标准。
所以我需要一些查询,它会立即以正确的顺序选择所有这些信息,因为目前我使用3个查询来做到这一点。
以下是我用来选择所需信息的查询,目前我选择所有项目将它们添加到数组中,然后我才根据距离对数组进行排序。
SELECT TOP COMPANIES
select CompanyInfo._id, CompanyInfo.name_en, ContactInfo.telephones, ContactInfo.location, CompanyInfo.websites, ContactInfo.address_en, ContactInfo.location, ContactInfo.position, TopFirmsByCode.Level, ContactInfo.name_en from CompanyInfo, ContactInfo, TopFirmsByCode where CompanyInfo._id=TopFirmsByCode.RegNo AND CompanyInfo._id=ContactInfo._id AND TopFirmsByCode.Code = 452 order by TopFirmsByCode.Level ASC, CompanyInfo.rating_ordering_en ASC`
选择按距离排列的其他公司
select CompanyInfo._id, CompanyInfo.name_en, ContactInfo.telephones,ContactInfo.location,CompanyInfo.websites,ContactInfo.address_en,ContactInfo.location, ContactInfo.position, ContactInfo.name_en from CompanyInfo, ContactInfo where CompanyInfo._id=ContactInfo._id AND CompanyInfo.category_codes LIKE %547% order by ContactInfo.location ASC
答案 0 :(得分:1)
尝试一下:
select CompanyInfo._id, CompanyInfo.name_en, ContactInfo.telephones,
ContactInfo.location, CompanyInfo.websites, ContactInfo.address_en,
ContactInfo.position, ContactInfo.name_en, TopFirmsByCode.Level,
(case when TopFirmsByCode.Code = 452 then 1 else 0 end) as isTopFirm
from CompanyInfo
join ContactInfo on (CompanyInfo._id=ContactInfo._id)
join TopFirmsByCode on (CompanyInfo._id=TopFirmsByCode.RegNo)
where (TopFirmsByCode.Code=452 OR CompanyInfo.category_codes LIKE %547%)
order by isTopFirm DESC, TopFirmsByCode.Level ASC, CompanyInfo.rating_ordering_en ASC