使用2个条件PHP PDO进行单词搜索

时间:2017-06-25 12:14:02

标签: php mysql search pdo

我想知道是否可以为我网站上的搜索引擎提供2个条件。 网站上的每个用户都有一个股票代码列表,目标是向他提供与每个用户拥有的股票列表相对应的文章。

例如,搜索引擎只搜索文章并显示文章中有2个或更多股票代码(如果文章只有一个股票代码,那么它也会显示它)

我很想知道是否有人知道该怎么做。提前致谢

用户编号1的库存清单是' aapl:us,spy:us,intc:us,goog:us' 然后只有超过2股的商品自动收报机将向他展示内部(如果文章中只提到一个股票,那么它也将显示给他)

 <?php
    $list = 'aapl:us,spy:us,intc:us,goog:us';
    $ticker = explode(",",$list);
    $count = count($ticker);
    $pha="";
    for($i=0;$i<$count;$i++){
    $pha.= " = ".$ticker[$i]." OR ";
    }  

$query = "SELECT * FROM table WHERE ticker ".$pha;
$result = $db->prepare($query); 
$result->execute(); 

while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '<li><a href="category.html?i='.$row['id'].'">'.$row['name'].'</a></li>';
}

enter image description here

1 个答案:

答案 0 :(得分:2)

利用REGEXP功能,可以解决您的问题

搜索

$list = 'aapl:us,spy:us,intc:us,goog:us';

$list = '(^|,)aapl:us|spy:us|intc:us|goog:us(,|$)';

# OR without modifying your list just replace comma with pipe
$list = 'aapl:us,spy:us,intc:us,goog:us';
$list = '(^|,)'.str_replace(',', '|', $list).'(,|$)'; 

$query = "SELECT * FROM table WHERE ticker ".$pha;

$query = "SELECT * FROM table WHERE ticker REGEXP $list";

将是

SELECT * FROM table WHERE ticker REGEXP (^|,)aapl:us|spy:us|intc:us|goog:us(,|$)

有特定数量的股票代码创建查询字符串,如下所示

SELECT *,((ticker REGEXP '(^|,)aapl:us(,|$)') + (ticker REGEXP '(^|,)spy:us(,|$)') ) as sum 
FROM 
        table 
HAVING sum = 2

测试总和以了解我们正在做什么

mysql> -- test variable
mysql> set @test='aapl:us,spy:us,intc:us,goog:us';
Query OK, 0 rows affected (0.00 sec)

mysql> -- variable contents
mysql> select @test;
+--------------------------------+
| @test                          |
+--------------------------------+
| aapl:us,spy:us,intc:us,goog:us |
+--------------------------------+
1 row in set (0.00 sec)

mysql> -- if you create sum it can be used in having clause
mysql> select ((@test REGEXP '(^|,)aapl:us(,|$)') + (@test REGEXP '(^|,)spy:us(,|$)') ) as sum;
+------+
| sum  |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

mysql> -- for 2 ticker
mysql> select * from( select ((@test REGEXP '(^|,)aapl:us(,|$)') + (@test REGEXP '(^|,)spy:us(,|$)') ) as sum ) as q having sum =2;
+------+
| sum  |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

mysql> -- for 3 or more ticker
mysql> select * from( select ((@test REGEXP '(^|,)aapl:us(,|$)') + (@test REGEXP '(^|,)spy:us(,|$)') ) as sum ) as q having sum >=3;
Empty set (0.00 sec)

生成查询的脚本

akshay@db-3325:/tmp$ cat test.php 
<?php

 $list = 'aapl:us,spy:us,intc:us,goog:us';

 $sum  = '('.implode("+", array_map(function($v){ return sprintf("(ticker REGEXP '(^|,)%s(,|$)')",$v); },explode(",",$list))).')';

 $query = "SELECT *, $sum as sum from table having sum = 2";

 print $query.PHP_EOL;
?>

<强>输出

akshay@db-3325:/tmp$ php test.php 
SELECT *, ((ticker REGEXP '(^|,)aapl:us(,|$)')+(ticker REGEXP '(^|,)spy:us(,|$)')+(ticker REGEXP '(^|,)intc:us(,|$)')+(ticker REGEXP '(^|,)goog:us(,|$)')) as sum from table having sum = 2
相关问题