我已经尝试了一段时间,但遇到了麻烦。
我们有 4 个表,包括供应商、supplier_areas、supplier_languages 和 supplier_products 选项。
我正在尝试进行高级搜索,用户可以使用上述任何一种搜索成员。
一个示例搜索可能是特定区域中讲英语和法语并且还销售产品 1 和 2 的所有供应商。
我知道必须首先查询位置表,然后是语言,然后是产品表,最后是供应商表中的特定字段。
例如
所有来自 supplier_areas 的 supplierid,其中 locationid = 1
例如,这会返回一个包含 supplierids '1'、'5'、'10' 的数组
然后我需要查询语言表以找出这些供应商中有哪些说英语,我能看到的唯一声明是
SELECT supplierid from supplier_languages WHERE languageid = 1 OR languageid = 2 AND supplierid = 1 OR supplierid = 5 OR supplierid = 10
然后显然是使用taht的结果来查询最后两张表。
我假设我计划执行的 OR 语句太慢并且服务器密集。第一个查询返回的结果最多可以是 200 多个供应商 ID。
如有任何帮助,我们将不胜感激。
谢谢
最佳答案
您可以将所有查询合并为一个:
select *
from supplier_areas
join supplier_languages using (supplierid)
join supplier_products using (supplierid)
join supplier using (supplierid)
where
supplier_areas.locationid=1
and supplier_languages.languageid in (1,2)
and supplier_products.productid in (....)
正如 middus 所说,深入研究 JOIN 语句..
关于PHP/SQL 使用之前查询的数据查询另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3955631/