[英]PHP / SQL Using data from previous query to query another table
我一直在努力工作一段時間,但遇到了麻煩。
我們有4個表,包括供應商,supplier_areas,supplier_languages和supplier_products選項。
我正在嘗試進行高級搜索,用戶可以使用上述任何一種搜索成員。
示例搜索可以是特定區域中的所有供應商,其使用英語和法語並且還銷售產品1和2。
我知道必須首先查詢位置表,然后是語言,然后是產品表,最后是供應商表中的特定字段。
例如
所有supplierid來自supplier_areas,其中locationid = 1
例如,這返回一個包含supplierids'1','5','10'的數組
然后,我需要查詢語言表,找出哪些供應商說英語,我能看到的唯一聲明是
來自supplier_languages的SELECT supplierid 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聲明。
您應該在mysql語句中考慮使用JOIN 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.