简体   繁体   English

搜索查询-邮政编码,公司名称或位置

[英]Search query - Postcode, Company name or Location

I am a bit stuck with SQL queries. 我对SQL查询有些困惑。

User should be able to search Postcode, Company name or Location 用户应能够搜索邮政编码,公司名称或位置

I have the following tables: 我有以下表格:

company table 公司表

companyid |     name        | location
      1      Shop One           New York
      2      Shop Two           France

postcode_areas table postcode_areas表

postcode | companyid
     BB1       1
     BB3       1
     BB1       2

So if user type in BB1 , then it should show the result Shop One, and Shop Two. 因此,如果用户输入BB1 ,那么它将显示结果Shop One和Shop Two。

If user type the name of company or location - it will just search from company table. 如果用户键入公司名称或位置-它将仅从公司表中搜索。

Use a join of both tables and do an OR-search on all fields: 使用两个表的联接并对所有字段进行“或”搜索:

SELECT DISTINCT c.* FROM company c JOIN postcode_areas p USING (companyid)
WHERE c.name = "$QUERY" OR c.location = "$QUERY" OR p.postcode = "$QUERY";

You might use LIKE "%$QUERY%" to get results for partial queries. 您可以使用LIKE "%$QUERY%"来获取部分查询的结果。

Maybe: 也许:

select distinct c.*
from company c
join postcode_areas p on p.company_id = c.company_id
where c.name like <input>
or c.location like <input>
or p.postcode like <input>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM