大家好,我已经进行了简单的搜索,我把SHOW COLUMN的所有专栏都列出来,而whilei做了像这样的简单条件
while($columns = mysql_fetch_assoc($res)){
if(!in_array($columns['Field'],$columns_a)){
$columns_a[] = 'a.`'.$columns['Field'].'`'.' LIKE \'%'.$_POST['search'].'%\'';
}
}
while($columns2 = mysql_fetch_assoc($res2)){
if(!in_array($columns2['Field'],$columns_a2)){
$columns_a2[] = 'b.`'.$columns2['Field'].'`'.' LIKE \'%'.$_POST['search'].'%\'';
}
}
$column_condition = implode(" OR ",columns_a);
$column_condition .= ' OR '.implode(" OR ",columns_a2);
所以在此之后我有很大的搜索条件。它对我的需求很有效,但是有问题,如果
,sql会给我两个结果$sql = mysql_query(
"SELECT *
FROM ".$prefix."orders a, ".$prefix."order_item b
WHERE ".$column_condition." AND a.order_id = b.order_id
ORDER BY a.order_id DESC",$connect);
LIKE
第一张表 - 订单
order_id | name
1 | Chesus
2 | Johny
第二张表 - order_item
order_id | order_item_id | product_name
1 | 999 | balsame
2 | 998 | better balsame
2 | 997 | worse_balsame
所以如果我搜索Johny它会给我2个结果而不是1个
答案 0 :(得分:0)
添加
group by a.order_id
到order by
之前的查询! :)
答案 1 :(得分:0)
使用正确的JOIN而不是隐式连接,然后您可以正确使用LIMIT语法
例如
$sql = mysql_query("
SELECT *
FROM ".$prefix."orders a
JOIN ".$prefix."order_item b on (a.order_id = b.order_id)
WHERE
".$column_condition."
ORDER BY a.order_id DESC
LIMIT 0,1
",$connect);