php - 错误代码 : 1172. 结果包含多行 [MySQL]

标签 php mysql mysqli mysql-workbench mysql-error-1064

这是我第一次在mysql中创建存储过程。

它的作用是,

first- get count of all records

second- loop through that table 1 by 1

third- compare each entry if it is a duplicate

fourth- insert duplicate in a temporary table

last- display duplicates

它可以在 100-200 个条目上正常工作但是在多达 500+(有时 25k)的更大记录上它会抛出一条消息

Error Code: 1172. Result consisted of more than one row

我已经用谷歌搜索了这个问题,但没有一个(答案)可以帮助我解决我的问题。

请看一下我的脚本

BEGIN

DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;

DECLARE i_sku VARCHAR(255);
DECLARE i_concatenated_attributes MEDIUMTEXT;

DECLARE f_sku VARCHAR(255);
DECLARE f_offer_type VARCHAR(255);
DECLARE f_name VARCHAR(255);
DECLARE f_product_owner VARCHAR(255);
DECLARE f_listing_city VARCHAR(255);
DECLARE f_listing_area VARCHAR(255);
DECLARE f_price DOUBLE;
DECLARE f_bedrooms INT;
DECLARE f_building_size INT;
DECLARE f_land_size INT;
DECLARE f_concatenated_attributes MEDIUMTEXT;
DECLARE f_duplicate_percentage INT;

SELECT COUNT(*) FROM unit_temp_listing INTO n;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_temp (dup_sku VARCHAR(255), dup_percentage INT, attribs MEDIUMTEXT);
SET i=0;
WHILE i<n DO 
    -- Get all unit listings (one by one)
    SELECT
    sku, concat_ws(',',offer_type,name,product_owner,listing_city,listing_area,price,ifnull(bedrooms,0),ifnull(building_size,0),ifnull(land_size,0)) as concatenated_attributes
INTO i_sku, i_concatenated_attributes
FROM unit_temp_listing
limit 1 offset i;
-- Compare one by one (sadla)
SELECT
    f.sku, f.offer_type, f.name, f.product_owner, f.listing_city, f.listing_area, f.price, f.bedrooms, f.building_size, f.land_size, 
    levenshtein_ratio(concat_ws(',',f.offer_type,f.name,f.product_owner,f.listing_city,f.listing_area,f.price,ifnull(f.bedrooms,0),ifnull(f.building_size,0),ifnull(f.land_size,0)),i_concatenated_attributes) as f_duplicate_percentage,
    concat_ws(',',f.offer_type,f.name,f.product_owner,f.listing_city,f.listing_area,f.price,ifnull(f.bedrooms,0),ifnull(f.building_size,0),ifnull(f.land_size,0)) as fconcatenated_attributes
INTO f_sku, f_offer_type, f_name, f_product_owner, f_listing_city, f_listing_area, f_price, f_bedrooms, f_building_size, f_land_size, f_duplicate_percentage, f_concatenated_attributes
FROM unit_temp_listing f
WHERE substring(soundex(concat_ws(',',offer_type,name,product_owner,listing_city,listing_area,price,ifnull(bedrooms,0),ifnull(building_size,0),ifnull(land_size,0))),1,10) = substring(soundex(i_concatenated_attributes),1,10) 
    AND levenshtein_ratio(concat_ws(',',offer_type,name,product_owner,listing_city,listing_area,price,ifnull(bedrooms,0),ifnull(building_size,0),ifnull(land_size,0)),i_concatenated_attributes) > 90
    AND f.sku != i_sku;
-- INSERT duplicates
IF(f_sku IS NOT NULL) THEN
    INSERT INTO temp_temp (dup_sku, dup_percentage, attribs) VALUES (f_sku, f_duplicate_percentage, f_concatenated_attributes);
    SET f_sku = null;
    SET f_duplicate_percentage = null;
    SET f_concatenated_attributes = null;
END IF;
SET i = i + 1;
END WHILE;
SELECT * FROM temp_temp;
DROP TABLE temp_temp;
End

问题是什么?

最佳答案

各位开发者 friend 们大家好我已经解决了我的问题,我想在这里分享。

问题是,我的 WHILE 循环内的 SECOND SELECT 语句返回多行。我尝试使用 CURSOR ,但仍然收到错误消息。因此,我尝试将我的第二个 SELECT 语句放入 INSERT 语句中,如下所示

INSERT INTO table_name (columns, ...) SELECT_STATEMENT

然后,问题就解决了!但如果这里有人有优化我的查询的想法,请帮助我。我必须处理 20k+ 条记录,但由于执行时间太长,我只满足于 15-20 分钟处理 500 条记录。

感谢您的 18 次浏览(在撰写本文时)。

编码愉快!

关于php - 错误代码 : 1172. 结果包含多行 [MySQL],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423510/

相关文章:

javascript - 在 Javascript 中使用单引号和双引号转义和取消转义字符串

php - 使用 PHP 和 MySQL 动态更改页面标题

MySQL子查询仅返回顶行结果

mysql - 如何选择不属于用户组的用户结果集

php - INSERT 到数据库后单引号和双引号显示为\' and\"

php - MySQL 中 NOT EXISTS 的替代方案,因为使用 NOT EXISTS 执行查询需要时间

javascript - jQuery - 将数组部分映射到 HTML

javascript - 管理页面在 orgfree 中不起作用 用户 'root' @'localhost' 的访问被拒绝?

php - 如果多个准备好的语句之一失败,则停止并恢复

javascript - 使用 AJAX 在点击时执行简单的 mysqli 查询