PHP / SQL 使用先前查询的数据来查询另一个表
我已经尝试这样做有一段时间了,但遇到了麻烦。
我们有 4 个表,包括供应商、供应商_区域、供应商_语言和供应商_产品选项。
我正在尝试进行高级搜索,用户可以使用上述任何一项来搜索成员。
示例搜索可以是某个区域中讲英语和英语的所有供应商。法语,还销售产品 1 和 2。
我知道始终必须首先查询位置表,然后是语言,然后是产品表,最后是供应商表中的特定字段。
例如,
来自supplier_areas 的所有supplierid,其中locationid = 1
例如,这将返回一个包含supplierids '1'、'5'、'10'的数组,
然后我需要查询语言表以找出这些供应商中哪些供应商会说英语,唯一的声明是我可以看到使用 is
SELECT sellerid from seller_languages WHERE languageid = 1 OR languageid = 2 AND Supplyid = 1 OR sellerd=5 OR sellerid = 10
然后显然使用 taht 的结果来查询最后两个表。
我假设我计划执行的 OR 语句会太慢并且会占用大量服务器。第一个查询返回的结果最多可以是 200 多个供应商 ID。
任何帮助将不胜感激。
谢谢
I've been trying to work this our for a while, but having trouble.
We have 4 tables consiting of, suppliers, supplier_areas, supplier_languages and supplier_products options.
I am trying to make an advanced search where users can search members using any of the above.
An example search may be all suppliers in a certain area that speak english & french and also sell products 1 and 2.
I know the locations table will always have to be queried first, followed by the languages, then the products table, and finally by specific fields out of the suppliers table.
E.g.
All supplierid's from supplier_areas where locationid = 1
This for example returns an array with the supplierids '1', '5', '10'
I then need to query the languages table to find out which of these suppliers speak english which the only statement I could see using is
SELECT supplierid from supplier_languages WHERE languageid = 1 OR languageid = 2 AND supplierid = 1 OR supplierid = 5 OR supplierid = 10
Then obviously use the result from taht to query the final two tables.
I'm assuming the OR statement that i'm planning on doing will be too slow and server intensive. The results returned from the first query could be anything upto 200+ supplier ids.
Any help would be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将所有查询合并为一个:
正如 middus 所说,深入研究 JOIN 语句。
you can combine all of the queries into one:
As middus already said, take a deep look into the JOIN statement..
您应该考虑在 mysql 语句中使用 JOIN 。
You should look into using JOIN in your mysql statements.