【问题标题】:Wildcard String Selection MySQL通配符字符串选择 MySQL
【发布时间】:2020-03-02 05:16:43
【问题描述】:

我有一张这样的桌子

|--------------------------------------------|------------------|
|      Column 1                              |     column 2     |
|--------------------------------------------|------------------|
|/fixed/sample_1                             |         10       |
|--------------------------------------------|------------------|
|/fixed/sample_1/                            |         15       |
|--------------------------------------------|------------------|
|/fixed/sample_1/sp1_level2                  |         10       |
|--------------------------------------------|------------------|
|/fixed/sample_1/sp1_level2/sp1_level3       |         20       |
|--------------------------------------------|------------------|
|/fixed/sample_2/                            |         25       |
|--------------------------------------------|------------------|
|/fixed/sample_2/sp2_level2/sp2_level3       |         20       |
|--------------------------------------------|------------------|
|/fixed/sample_3                             |         30       |
|--------------------------------------------|------------------|

我想使用 select 语句从第 1 列中选择并为第 2 列添加值,我的结果表是

|--------------------------------------------|------------------|
|      Column 1                              |     column 2     |
|--------------------------------------------|------------------|
|/fixed/sample_1/                            |         55       |
|--------------------------------------------|------------------|
|/fixed/sample_2/                            |         45       |
|--------------------------------------------|------------------|
|/fixed/sample_3/                            |         30       |
|--------------------------------------------|------------------|

有什么办法吗?

谢谢

【问题讨论】:

    标签: mysql sql string select wildcard


    【解决方案1】:

    我认为你可以使用substring_index()group by

    select substring_index(col1, '/', 3) as new_col1,
           sum(col2)
    from t
    group by new_col1;
    

    【讨论】:

    • 您的回答似乎是正确的 Gordon,但我意识到 GoogleSQL 不等同于 MySQL,并且它没有 substring_index 函数。还有其他方法可以做到吗?谢谢
    • @ManojAgrawal 。 . .我不知道您所说的 GoogleSQL 是什么意思。如果您指的是 BigQuery,请提出一个新问题并使用 BigQuery 进行标记。
    猜你喜欢
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 2012-03-22
    • 2013-07-12
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多