简体   繁体   English

高级MySQL搜索查询-使用RLIKE

[英]Advanced MySQL search query - using RLIKE

Im working on a MySQL search query for a product search module in Joomla/Virtuemart. 我正在Joomla / Virtuemart中针对产品搜索模块的MySQL搜索查询进行工作。 I am actually trying to modify an existing MySQL query from using MATCH / AGAINST to using RLIKE, but my modified query gives errors.. 我实际上正在尝试将现有的MySQL查询从使用MATCH / AGAINST修改为使用RLIKE,但是我修改后的查询给出了错误。

Here is the original query with MATCH / AGAINST which has no errors: 这是使用MATCH / AGAINST的原始查询,没有错误:

$searchstring = " +search* +string* +test*";
$query ="SELECT p.virtuemart_product_id, l.product_name from #__virtuemart_products AS p, #__virtuemart_products_".VMLANG." AS l WHERE MATCH(product_name,customtitle) AGAINST ('".$searchstring."' IN BOOLEAN MODE) AND p.published = '1' AND p.virtuemart_product_id = l.virtuemart_product_id  LIMIT 0,".$prods." union (select p.virtuemart_product_id, l.product_name from #__virtuemart_products AS p, #__virtuemart_products_".VMLANG." as l where  MATCH(product_sku) AGAINST ('".$searchstring."' IN BOOLEAN MODE) and p.published = '1'  and p.virtuemart_product_id = l.virtuemart_product_id LIMIT 0,".$prods.")";

Here is my altered query using RLIKE: 这是我使用RLIKE更改的查询:

$searchstring = "search|string|test";
$query ="SELECT p.virtuemart_product_id, l.product_name from #__virtuemart_products AS p, #__virtuemart_products_".VMLANG." AS l WHERE product_name,customtitle RLIKE '".$searchstring."' AND p.published = '1' AND p.virtuemart_product_id = l.virtuemart_product_id LIMIT 0,".$prods." union (select p.virtuemart_product_id, l.product_name from #__virtuemart_products AS p, #__virtuemart_products_".VMLANG." as l where product_sku RLIKE '".$searchstring."' and p.published = '1'  and p.virtuemart_product_id = l.virtuemart_product_id LIMIT 0,".$prods.")";

I am out of ideas as to why the RLIKE search query does not work. 我对RLIKE搜索查询为什么不起作用一无所知。 I hope someone can point out what I´m doing wrong here.. 我希望有人可以指出我在这里做错了什么。

You have this weird expression in your query: 您的查询中有以下奇怪的表达式:

WHERE product_name, customtitle RLIKE '".$searchstring."'

Try combining them using concat() first: 尝试首先使用concat()组合它们:

(SELECT p.virtuemart_product_id, l.product_name
 from #__virtuemart_products p join
      #__virtuemart_products_".VMLANG." l
      on p.virtuemart_product_id = l.virtuemart_product_id
 WHERE concat(product_name, customtitle) RLIKE '".$searchstring."' AND
       p.published = '1'
 LIMIT 0,".$prods."
)
union
(select p.virtuemart_product_id, l.product_name
 from #__virtuemart_products p join
      #__virtuemart_products_".VMLANG." l
      on p.virtuemart_product_id = l.virtuemart_product_id
 where product_sku RLIKE '".$searchstring."' and
       p.published = '1'
 LIMIT 0,".$prods.
)

You could also compare each one separately: 您还可以分别比较每个:

 WHERE (product_name RLIKE '".$searchstring."' OR
        customtitle RLIKE '".$searchstring."' 
       ) AND . . .

Note that I also fixed the join syntax to use explicit joins. 请注意,我还修复了join语法以使用显式连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM