这是我的例子。当我运行此查询时:
SELECT SUBSTRING('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.', 1, 300)
我得到了句子的前300个字符。那很好。除了我希望它结束最后一个点(。)在结果中的位置。
所以查询结果如下:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convalli
但我希望它是这样的:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor.
我可以使用MySQL查询,以及如何做到这一点?
TNX
更新1, 这是我到目前为止所做的:
SET @longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.';
SELECT SUBSTRING_INDEX(
@longtext,
SUBSTRING_INDEX(
SUBSTRING(
@longtext,
1,
300
),
'.',
-1
)
, 1
);
更新2, Tnx @Ben
我的解决方案是:
SET @longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.';
SELECT SUBSTRING(
@longtext,
1,
300 - ifnull(
LOCATE(
'.',
REVERSE(
SUBSTRING(
@longtext,
1,
300
)
)
),
1)
);
答案 0 :(得分:2)
我试过这个,但我认为有点复杂,但它确实有效。
DECLARE @BEGIN INT;
DECLARE @END INT;
DECLARE @TEXT VARCHAR(MAX);
DECLARE @DELIMITER VARCHAR(MAX);
SET @BEGIN = 1;
SET @END = 300;
SET @DELIMITER = '.'
SET @TEXT = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.'
--SELECT SUBSTRING(@TEXT, @END-1, 1)
WHILE SUBSTRING(@TEXT, @END, 1) <> @DELIMITER
BEGIN
IF SUBSTRING(@TEXT, @END-1, 1) = @DELIMITER
BEGIN
SELECT SUBSTRING(@TEXT, @BEGIN, @END-1)
BREAK;
END
ELSE
BEGIN
SET @END = @END -1
CONTINUE
END
END
答案 1 :(得分:1)
select reverse( substring( reverse( 'Lorem String' ), locate( '.', reverse( 'Lorem String' ) ) ) )
其中'Lorem String'是你上面的大字符串。
答案 2 :(得分:1)
我桌上的一个工作示例:
SELECT substr(notes,1,(301-(LOCATE('。',REVERSE(substr(notes,1,300))))))FROM帐户,其中id ='4'
我的数据库中的原始文本字符串,包含480个字符(32个字符的附件x 15个条目)
"This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 31 characters. This sentence is 32 characters. This sentence is 32 characters. "
查询结果是287个字符,因为第10个句子太长而不适合包含的句号:
"This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters. This sentence is 32 characters."
丑陋..但它确实有效。
答案 3 :(得分:0)
SELECT SUBSTRING_INDEX(SUBSTRING('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.', 1, 300),'.',-1) + '.'
答案 4 :(得分:0)
好的,所以我想我已经得到了它;遗憾的是,由于事物的性质,评论反过来......
select substr('my_string', 1
-- 300 - distance from the first '.' in the reversed string.
, 300 - ifnull(
-- Get the 1st position of the '.' in the
-- reversed string.
locate('.', reverse(substr('my_string',1,300))
)
-- if it's null, i.e. doesn't exist, replace it with 1.
, 1)
)
您可以根据需要更改300.