【问题标题】:Make SQL query that rename output entries进行重命名输出条目的 SQL 查询
【发布时间】:2014-09-28 10:07:44
【问题描述】:

我有一个带有字段 category 的表格,其中包含如下条目:

germany_bundesliga
germany_2nd_bundesliga
england_premier_leauge
england_championship
spain_liga
spain_liga_adelante

等等……

我想得到这个输出:

gemany
england
spain
...

是否可以使用 SQL 查询?

【问题讨论】:

  • 这适用于 SQL Server、MySQL 或任何其他数据库吗?
  • MySQL...我忘了说,对不起

标签: mysql sql substring


【解决方案1】:

这就是你想要的吧?

select distinct SUBSTRING_INDEX(value,'_',1) from category;

SQLFIDDLE

【讨论】:

    【解决方案2】:

    想一想……

    SELECT SUBSTRING_INDEX('germany_2nd_bundesliga','_',1);
    

    【讨论】:

      【解决方案3】:

      虽然这并不完全漂亮,但在你的情况下你可以做到

      select distinct substr(category, 0, indexof(category)) from TABLE;
      

      要点

      【讨论】:

      • #1630 - FUNCTION substr 不存在。检查参考手册中的“函数名称解析和解析”部分
      • 我也尝试使用 substring 代替 substr 但我得到 indexof 的错误:#1305 - FUNCTION indexof 不存在
      • 它没有否决您的答案,是其他人做到的。
      猜你喜欢
      • 1970-01-01
      • 2021-01-24
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 2011-12-16
      • 2016-01-19
      相关资源
      最近更新 更多