选择一个字段的不同的首字母 [英] Select distinct first words of a field
问题描述
我希望能够进行选择查询,只接受字符串中不同的前几个词,但不包括最后一个.如果不清楚,这是下表希望得到的结果:
i would like to be able to do a select query, taking only the distinct first words of the string, excluding the last one. If not clear, here is the result i'd like to have for the following table :
Apple iPhone 4S
苹果iPhone 4
苹果iPhone 6 Plus
Apple iPhone 4S
Apple iPhone 4
Apple iPhone 6 Plus
+-----------+--------------------------------+
| id | model |
+-----------+--------------------------------+
| 1 | Apple iPhone 4S 16Gb |
| 2 | Apple iPhone 4S 32Gb |
| 3 | Apple iPhone 4 8Gb |
| 4 | Apple iPhone 6 Plus 32Gb |
| 5 | Apple iPhone 6 Plus 64Gb |
+-----------+--------------------------------+
如何在排除最后一个空格后的值的同时选择字段的不同值?
How to select the distinct values of the field while excluding the values after the last space ?
我试图找到有关函数的文档,例如
right(model,lastindexof(")).
预先感谢您的帮助:)
I tried to find documentation about functions like a sort of
right(model, lastindexof(" ")).
Thank you in advance for your help :)
推荐答案
您可以反转字符串以找到第一个空格" "
You can reverse the string to find the first space " "
SQL Demo
SELECT DISTINCT RTRIM(REVERSE(SUBSTRING(REVERSE(`model`),LOCATE(" ",REVERSE(`model`)))))
FROM Table1
输出
这篇关于选择一个字段的不同的首字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!