【问题标题】:Word search with 2 conditions PHP PDO带 2 个条件 PHP PDO 的词搜索
【发布时间】:2017-11-28 12:00:00
【问题描述】:

我想知道是否可以为我网站上的搜索引擎提供 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>';
}

【问题讨论】:

    标签: php mysql search pdo


    【解决方案1】:

    利用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
    

    【讨论】:

    • 搜索引擎将搜索文章并仅在文章中有 2 个或更多股票代码时显示(如果文章只有一个股票代码,它也会显示) ? @akshay TIA
    • @AnnaLA 看到我编辑了我的答案,我猜这可以解决
    • 嗨...它不工作 "Array ( [0] => 42000 [1] => 1064 [2] => 您的 SQL 语法有错误;请查看对应的手册您的 MariaDB 服务器版本,以便在第 1 行的 '^|,)aapl:us|spy:us|intc:us|goog:us(,|$)' 附近使用正确的语法)"
    • @AnnaLA,见第二部分,其中每个单词将被汇总
    • @AnnaLA 添加了 php 脚本,它也会生成查询字符串
    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 2017-09-26
    • 2014-05-21
    • 2016-07-15
    • 2019-08-21
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多