嗨,朋友们,我在我的网站上有一个自定义搜索功能。搜索取决于两个表,例如, product_table使用自定义搜索选项在两个表中搜索mysql数据
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对于这样做有什么意义吗?还是我有任何简单的方法来实现这一点?感谢
感谢LitterWhite现在的作品。大! – vinu