带2个条件的单词搜索PHP PDO

带2个条件的单词搜索PHP PDO,php,mysql,search,pdo,Php,Mysql,Search,Pdo,我想知道是否有可能给我的网站上的搜索引擎2个条件。 网站上的每个用户都有一个股票行情列表,目标是向他展示与每个用户的股票列表相对应的文章 例如,搜索引擎将搜索文章并仅在文章中有两个或更多股票代码时显示(如果文章只有一个股票代码,则也将显示) 我很想知道是否有人知道怎么做。提前谢谢 用户编号1的股票列表是“aapl:us,spy:us,intc:us,goog:us”,然后只有包含2股以上股票的文章才会显示给他(如果文章中只提到一只股票,那么也会显示给他) 利用REGEXP函数,它可以解决您的问题

我想知道是否有可能给我的网站上的搜索引擎2个条件。 网站上的每个用户都有一个股票行情列表,目标是向他展示与每个用户的股票列表相对应的文章


例如,搜索引擎将搜索文章并仅在文章中有两个或更多股票代码时显示(如果文章只有一个股票代码,则也将显示)

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

用户编号1的股票列表是“aapl:us,spy:us,intc:us,goog:us”,然后只有包含2股以上股票的文章才会显示给他(如果文章中只提到一只股票,那么也会显示给他)


利用
REGEXP
函数,它可以解决您的问题

用于搜索

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)

那是什么

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;
?>

搜索引擎将搜索文章,仅当文章中有2个或更多股票代码时才会显示(如果文章只有一个股票代码,则也会显示)@阿克什TIA@AnnaLA看,我编辑了我的答案,这可以解决我猜。。。“数组([0]=>42000[1]=>1064[2]=>您的SQL语法有错误;请查看与您的MariaDB服务器版本对应的手册,以了解在“^ |,”)附近使用的正确语法aapl:us | spy:us | intc:us | goog:us(,|$)”@AnnaLA,请参阅第二部分,其中每个单词都将summed@AnnaLA还添加了将生成查询字符串的php脚本
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