选择一个字段的不同的首字母 [英] Select distinct first words of a field

查看:64
本文介绍了选择一个字段的不同的首字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够进行选择查询,只接受字符串中不同的前几个词,但不包括最后一个.如果不清楚,这是下表希望得到的结果:

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演示

SQL Demo

SELECT DISTINCT RTRIM(REVERSE(SUBSTRING(REVERSE(`model`),LOCATE(" ",REVERSE(`model`)))))
FROM Table1

输出

这篇关于选择一个字段的不同的首字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆