使用自定义搜索选项在两个表中搜索mysql数据

时间:2012-09-27 03:07:08

标签: php mysql search

朋友,我在我的网站上有自定义搜索功能。搜索取决于两个表,如, 的 product_table

CREATE TABLE `product_table` (
 `product_id` int(11) NOT NULL auto_increment,
 `product_name` varchar(100) NOT NULL,
 `country` varchar(200) NOT NULL,
 `added_time` timestamp NOT NULL default CURRENT_TIMESTAMP,
 PRIMARY KEY  (`product_id`));

multiple_table

CREATE TABLE `multiple_table` (
 `multi_id` int(11) NOT NULL auto_increment,
 `cat_id` int(11) NOT NULL,
 `product_id` int(11) NOT NULL,
 PRIMARY KEY  (`multi_id`));

我通过一个或多个字段为用户提供搜索选项product_name,country,cat_id,added_time

我的搜索功能如下,

if(isset($_GET['s_button'])){
$sql2="SELECT a.product_id FROM product_table a, multiple_table b WHERE b.product_id= a.product_id ";
$where=array();
$values = array();
$types = '';
if(!empty($_GET['search_cat'])){
$search_cat=clean($_GET['search_cat']);
$where[]="AND cat_id='".$search_cat."'";
}
if(!empty($_GET['product_country'])){
$country=clean($_GET['product_country']);
$where[]="AND country='".$country."'";
}
if(!empty($_GET['model'])){
$keyword=clean($_GET['model']);
$where[]="AND product_name LIKE '%".$keyword."%'";
}
$sql2 .= implode($where). " ORDER BY a.product_id ";
}else{
$sql2 = "SELECT product_id FROM product_table ORDER BY Rand()";
}

但这会产生一些不正确和大量的结果,而且搜索也需要花费太多时间。我不知道为什么会这样。我听说过MYSQL VIEWs这样做有什么意义吗?或者我有什么简单的方法来实现这一目标?感谢

2 个答案:

答案 0 :(得分:1)

你应该这样试试:

SELECT a.product_id FROM product_table a INNER JOIN multiple_table b ON b.product_id= a.product_id

答案 1 :(得分:0)

考虑类似的事情:

<?php
if(isset($_GET['s_button'])){
    $options = array(
        (object)array(
            "get_key"=>"search_cat",
            "table_key"=>"cat_id"
        ),
        (object)array(
            "get_key"=>"product_country",
            "table_key"=>"country"
        ),
        (object)array(
            "get_key"=>"model",
            "table_key"=>"product_name",
            "compare"=>"LIKE"
        )
    );
    $parseOptions = function($element){
        $defaults = array("wildcards"=>"both");
        $settings = (object)array_merge($defaults, (array)$element);
        if(isset($_GET[$settings->get_key]) && strlen($_GET[$settings->get_key])>0){
            $search = mysql_real_escape_string($_GET[$settings->get_key]);
            switch($settings->compare){
                default:
                    return $settings->table_key."='$search'";
                    break;
                case 'LIKE':
                    switch($settings->wildcards){
                        case 'both':
                            return $settings->table_key." LIKE '%$search%'";
                            break;
                    }
            }
        }
    };
    $where = array_map($parseOptions, $options);
    $sql2="SELECT
        products.product_id
        FROM product_table AS products
        LEFT JOIN multiple_table AS multi
            ON products.product_id = multi.product_id";

    if(count($where)>0) $sql2 .= " WHERE ".implode(" AND ",$where);
    $sql2 .= " ORDER BY product_id ";
}else{
    $sql2 = "SELECT product_id FROM product_table ORDER BY Rand()";
}

扩展起来会更容易。