PHP / SQL 使用先前查询的数据来查询另一个表

发布于 2024-09-28 08:39:57 字数 648 浏览 0 评论 0原文

我已经尝试这样做有一段时间了,但遇到了麻烦。

我们有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身软味 2024-10-05 08:39:57

您可以将所有查询合并为一个:

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 语句。

you can combine all of the queries into one:

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 (....)

As middus already said, take a deep look into the JOIN statement..

小兔几 2024-10-05 08:39:57

您应该考虑在 mysql 语句中使用 JOIN

You should look into using JOIN in your mysql statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文