down2323 2010-10-17 22:42
浏览 48
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • dongying7847 2010-10-17 22:48
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积