1

我环顾四周,看看是否能找到直接解决我的问题的解决方案,但我似乎找不到。

我正在查询我的旧数据库并创建一个可以导入新数据库的新表。我想创建一个名为的新字段,该字段news_releases_summary派生自news_releases_body,但仅包含前 160 个字符(用于 SEO 元描述),理想情况下没有 HTML。

我当前的查询看起来像这样。我遇到问题的功能是 LTRIM RTRIM - 我什至不确定它是否适合这项工作?* 请注意,我还需要删除 \n\t,因为如果我不这样做,最终输出将不会验证为 JSON。

SELECT  
FROM_UNIXTIME(exp_channel_titles.entry_date,'%m/%e/%Y %h:%i %p') AS entry_date,
exp_channel_titles.status, 
exp_channel_titles.title, 
exp_channel_data.field_id_66 AS news_releases_subtitle, 
exp_channel_data.field_id_65 AS news_releases_contact, 
REPLACE(replace(exp_channel_data.field_id_67,char(10),''),char(13),'') AS news_releases_body
LTRIM(RTRIM(REPLACE(replace(exp_channel_data.field_id_67,char(10),''),char(13),''))) AS news_releases_summary
FROM exp_channel_data INNER JOIN exp_channel_titles 
ON exp_channel_data.entry_id = exp_channel_titles.entry_id 
WHERE exp_channel_data.channel_id = '21' 
ORDER BY exp_channel_titles.entry_date

更新

这就是我现在所在的位置,但我收到错误“FUNCTION ee_2.strip_tags 不存在”。我应该直接在查询中包含 strip_tags 函数吗?我在 Mac 上使用 Sequel Pro。

CREATE FUNCTION `strip_tags`($str text) RETURNS text
BEGIN
    DECLARE $start, $end INT DEFAULT 1;
    LOOP
        SET $start = LOCATE("<", $str, $start);
        IF (!$start) THEN RETURN $str; END IF;
        SET $end = LOCATE(">", $str, $start);
        IF (!$end) THEN SET $end = $start; END IF;
        SET $str = INSERT($str, $start, $end - $start + 1, "");
    END LOOP;
END;

SELECT  
FROM_UNIXTIME(exp_channel_titles.entry_date,'%m/%e/%Y %h:%i %p') AS entry_date,
exp_channel_titles.status, 
exp_channel_titles.title, 
exp_channel_data.field_id_66 AS news_releases_subtitle, 
exp_channel_data.field_id_65 AS news_releases_contact, 
REPLACE(REPLACE(exp_channel_data.field_id_67,char(10),''),char(13),'') AS news_releases_body,
LEFT(strip_tags(REPLACE(REPLACE(exp_channel_data.field_id_67,char(10),''),char(13),'')),160)     AS news_releases_summary 
FROM exp_channel_data INNER JOIN exp_channel_titles 
ON exp_channel_data.entry_id = exp_channel_titles.entry_id 
WHERE exp_channel_data.channel_id = '21' 
ORDER BY exp_channel_titles.entry_date
4

1 回答 1

1

您应该能够使用嵌套replace调用来去除不需要的换行符;尽管他们使用文字\n不是\r使用char已经

对于剥离 HTML 实体,如果不滚动您的函数来执行业务,那么就没有一种简单的方法可以做到这一点。谢天谢地,有人也为你做到了

最后,如果您只希望结果中包含 160 个字符,请使用该left函数将结果限制为最左侧的 160 个字符。

于 2014-06-20T15:31:32.057 回答