我正在尝试为我的搜索引擎的MySQL查询索引项目列表。
我们假设我在其他地方定义了这个项目列表:
现在让我们假设我有这样的查询:
SELECT
home.id AS id,
home.title AS title,
SUBSTRING(all_items.value, 1, 100) items
FROM table home
LEFT JOIN content_values all_items ON
all_items.id = home.id AND all_items.varid = 81
WHERE
home.deleted = 0 AND home.published = 1
导致此输出:
ID,pagetitle,items
01,option1,1234 || 4567
02,option2,1234 || 7890
最初,我试图从该字符串中取出每个项目,并使用 SUBSTRING_INDEX()将其添加为每个值的新列。哪个有效,但我需要为我想在搜索索引器中记录的每个值手动添加一组新列。
有没有办法可以“翻译”“||”之间的字符串值分隔符包含值名称而不是?所以在索引中它只读取tree||car
而不是1234||4567
而tree||house
而不是1234||7890
用于另一条记录?
样本数据集:
CREATE TABLE IF NOT EXISTS table (
id int unsigned NOT NULL,
pagetitle varchar(200) NOT NULL,
UID int unsigned NOT NULL,
deleted int unsigned NOT NULL,
published int unsigned NOT NULL,
PRIMARY KEY (id,UID)
);
INSERT INTO table (id, pagetitle, UID, deleted, published) VALUES
(1, 'tree', 1234, 0, 1),
(2, 'car', 4567, 0, 1),
(3, 'house',7890, 0, 1);
CREATE TABLE IF NOT EXISTS content_values (
id int unsigned NOT NULL,
name varchar(200) NOT NULL,
varid varchar(200) NOT NULL,
PRIMARY KEY (id,varid)
);
INSERT INTO content_values (id, name, varid) VALUES
(11, 'option1', '1234||4567'),
(12, 'option2', '1234||7890');
和[sqlfiddle](sqlfiddle.com/#!9/8812b/2)同样的