1

I am looking for the most efficient way to do the following.

I have "table1" with the following columns, ID SavedSearch Price NewLowerPrice

what i need to do is select id and price from table 1, and flag new lower price if the boolean search against another table has a new lower min price for every saved search by the user. here is some code I am using, but i am stuck as to how to use table1's saved search in the following boolean search

drop temporary table if exists tmp;

CREATE TEMPORARY TABLE tmp(
ID INT,
Param2 Varchar(300),
LowestPrice decimal(10,2),
LowerPrice bit,
PRIMARY KEY(ID)) ENGINE=MEMORY;

INSERT INTO tmp
SELECT id, Param2, LowestPrice, 0
FROM saved_searches
where user = 28;


UPDATE tmp
set LowerPrice = 1
WHERE (
SELECT MIN(price)
FROM store_sales 
WHERE MATCH (description) AGAINST (concat('\'', tmp.Param2, '\'') IN BOOLEAN MODE)) > 0;

it errors out in the update saying "incorrect arguments to AGAINST." thanks in advance!

4

1 回答 1

1

从逻辑上讲,您正在做一些应该有意义的事情,但不幸的是,MySQL 的 AGAINST() 函数不允许您引用搜索字符串中的列。参数必须是固定字符串,否则是查询参数

http://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html说:

搜索字符串必须是在查询评估期间保持不变的字符串值。例如,这排除了表格列,因为每行可能不同。

你必须以艰难的方式做到这一点:

  1. 查询所有不同的搜索参数:

    SELECT DISTINCT Param2 FROM saved_searches;
    
  2. 对于每个 Param2 值,查询最低价格:

    SELECT MIN(price)
    FROM store_sales 
    WHERE MATCH (description) AGAINST (:param2) IN BOOLEAN MODE)) > 0;
    
  3. 当最低价格低于当前记录的相应 param2 搜索词的最低价格时,更新您的 tmp 表。

    UPDATE tmp
    set LowerPrice = 1
    WHERE LowestPrice > :minPrice Param2 = :param2
    

我在上面的例子中使用了命名参数,就像你对 PHP 的 PDO 所做的那样。但在存储过程语言中,您只能将位置参数与?占位符一起使用。

编写循环遍历查询结果并在循环内运行内部查询的存储过程可能很棘手。这是一个很好的博客,展示了如何做到这一点:Roland Bouman 的 Nesting MySQL Cursor Loops

于 2013-11-13T16:54:16.300 回答