简体   繁体   English

从MySQL表中的一个字段中选择一个不同的子字符串列表

[英]Select a distinct list of substrings from one of the fields in a MySQL table

I have rows such as 我有像这样的行

  • [ISSUE] This is a sample issue [问题]这是一个示例问题
  • [WEBSITE] A bug in the website [网站]网站上的一个错误
  • Note that this row doesn't contain anything inside square brackets 请注意,此行不包含方括号内的任何内容
  • [WEBSITE] Another bug in the website [网站]网站上的另一个错误
  • [TRACKER] It is not live! [追踪者]它不是活的!
  • Some rows will have them in the [middle] but I don't want them 有些行会在[中]有它们,但我不想要它们

etc.. 等等..

Assume the field is called "Title" and the table is called "Issues". 假设该字段称为“标题”,该表称为“问题”。 All items in that column start with a string within square brackets. 该列中的所有项目都以方括号内的字符串开头。 There is no definitive list of what words can come inside the square brackets. 方括号内没有明确的单词列表。 no restriction on the length also. 对长度也没有限制。 I need not get it if the field doesn't start with such a string. 如果字段没有以这样的字符串开头,我不需要它。

I want to get the word inside the pair of square brackets and get a unique list of those words. 我想在方括号内得到这个词,并获得这些词的唯一列表。

I want to select a distinct list of all the [XYZ]s. 我想选择所有[XYZ]的不同列表。 For example, in the list above, the query should return the following list of strings: 例如,在上面的列表中,查询应返回以下字符串列表:

  • ISSUE 问题
  • WEBSITE 网站
  • TRACKER TRACKER

It is a combination of substring logic, and distinct query. 它是子串逻辑和不同查询的组合。 The logic might go like Find the index of the first [ and the first ] and get the string between that and from the list, make a unique list . 逻辑可能就像查找第一个[和第一个]的索引一样,并从列表中获取字符串,创建一个唯一的列表 I am finding it nearly impossible to write it in a single MySQL query. 我发现几乎不可能在一个MySQL查询中编写它。 What is the query that will get that list? 获得该列表的查询是什么?

If I understand correctly, you have a table which contains a field named Title. 如果我理解正确,你有一个包含名为Title的字段的表。 The title may start with the string you are looking for and you only want it if it starts with the string. 标题可能以您要查找的字符串开头,如果它以字符串开头,您只需要它。 You also want the result to be distinct. 您还希望结果是不同的。

This query will do that. 此查询将执行此操作。

SELECT Issue, Website, Tracker FROM `Issues` Where distinct(Title) LIKE '[SOMETHING]%'

In that case I would try: 在那种情况下,我会尝试:

select distinct `Match` from (SELECT MID(Title,instr(Title,'[') + 1,instr(Title,']') - instr(Title,'[') -1) As 'New_Title'
FROM Issues u Where instr(Title,'[') > 0 AND instr(Title,']') > instr((Title),'[')) tbl

Sorry this took so long, I had to figure out how to return the string. 对不起,这花了这么长时间,我不得不弄清楚如何返回字符串。

SELECT distinct(标题REGEXP'^ [[]'然后concat(substring_index(title,']',1),']')结束时的情况作为标题FROM test.issue i其中标题REGEXP'^ [[]';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM