combine search from multiple tables

时间:2015-04-23 05:39:30

标签: php mysql

The current table structure we have is

product_description [
    (INT)product_id, 
    (VARCHAR)name, 
    (TEXT)description, ...
]
vendors [
    (INT)vendor_id, 
    (VARCHAR)vendor_name, 
    (TEXT)vendor_description, ...
]
vendor [
    (INT)vendor, 
    (INT)product_id, ...
]

Currently we have a search option where we fetch matching values from both tables. They work independently, i.e. keyword search with LIKE gets matching value from both tables' name as well as description fields.

Now the requirement has been changed by client. Now the client wants the search to be dependent, i.e. suppose some one search for product then their associated vendors should be fetched and if vendor is searched then associated products should be visible. But the issue is there is no option to determine whether user is searching for product or vendor.

Is there any way to make the search dependent?

After search there is also other filter to work out like location, price range, etc. Also the result is displayed on a tab based page where separate tab for vendor and product and each tab has their own pagination which do not affect the other tab.

We suggested to the client to give a drop down like elance does near their search bar so that user can specify what they want to search for, but changing design is not possible now. How to proceed with the searching?

1 个答案:

答案 0 :(得分:0)

经过一番说服后,我们能够修改客户端的要求,以解决实施问题并在搜索时提供适当的价值。根据提供的新搜索指南,条件是:

  1. 如果<关键字> == PRODUCT,然后在PRODUCT标签上显示PRODUCT,以及销售这些产品的相关供应商以及<关键字> VENDORS的匹配。

  2. 如果<关键字> ==供应商,然后在供应商选项卡上显示卖家,并且只有<关键字>产品匹配。

  3. 基本上,供应商依赖,而产品将是独立的。

    产品的查询 -

    SELECT 
        p.product_id, 
        pd.product_type, 
        (SELECT AVG(rating) AS total FROM review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) 
            AS 
            rating, 
        (SELECT price FROM product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '1' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) 
            AS 
            discount,
        (SELECT price FROM product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) 
            AS 
            special 
    FROM product p 
    LEFT JOIN product_description pd 
        ON (p.product_id = pd.product_id) 
    LEFT JOIN product_to_store p2s 
        ON (p.product_id = p2s.product_id) 
    WHERE pd.language_id = '1' 
        AND p.status = '1' 
        AND p.date_available <= NOW() 
        AND p2s.store_id = '0' 
        AND ( pd.name LIKE '%<KEYWORD>%' 
            OR 
            pd.description LIKE '%<KEYWORD>%' 
            OR 
            pd.tag LIKE '%<KEYWORD>%' 
            OR 
            LCASE(p.model) = '<KEYWORD>' 
            ) 
    GROUP BY p.product_id 
    ORDER BY p.image DESC, LCASE(pd.name) 
    ASC 
    LIMIT 0,15 
    

    供应商使用的查询 -

    SELECT 
         DISTINCT (vds.vendor_id), 
         vds . * 
     FROM vendors vds
     LEFT JOIN user u 
          ON ( vds.user_id = u.user_id ) 
     LEFT JOIN vendor v
          ON vds.vendor_id = v.vendor
     LEFT JOIN product_description pd
          ON v.vproduct_id = pd.product_id
     LEFT JOIN product p 
          ON v.vproduct_id = p.product_id
     WHERE u.status = '1'
          AND p.status = '1'
          AND vds.display_vendor = '1'
          AND (
                LOWER( vds.vendor_name ) LIKE LOWER( "%<KEYWORD>%" ) 
                OR 
                LOWER( vds.vendor_description ) LIKE LOWER( "%<KEYWORD>%" ) 
                OR 
                LOWER( pd.name ) LIKE LOWER( "%<KEYWORD>%" ) 
                OR
                LOWER( pd.description ) LIKE LOWER( "%<KEYWORD>%" ) 
           )
     ORDER BY vds.vendor_name 
     ASC 
     LIMIT 0 , 30