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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了