【问题标题】:Get dynamic column value获取动态列值
【发布时间】:2013-03-15 13:43:31
【问题描述】:

我有一个表学生,其中包含:

  student_id , prefered_cell , cell_1 , cell_2 , cell_3 

现在在我们拥有的首选单元格值下 (cell_1,cell2,cell3)

在 mysql 中获取首选单元格值的最佳方法是什么?

这些是可用的行:

1 , 'cell_1' ,  '123-321-1231' , '431-2133-321' , '123-123-4111'
2 , 'cell_2' ,  '122-333-3333' , '455-123-123' , '123-421-2345'

所以我想要学生 1 和 2 的首选号码:

会得到:

1 , '123-321-1231'
2 , '455-123-123'

【问题讨论】:

  • 请发布一些示例数据和所需的输出。
  • 你的问题不清楚。
  • @jcho360 更新了我的问题

标签: mysql subquery concat


【解决方案1】:

试试这个查询。

Select student_id, case when prefered_cell = 'cell_1' then cell_1 
                        when prefered_cell = 'cell_2' then cell_2
                        when prefered_cell = 'cell_3' then cell_3
                        else '0'
                        End  as cell_value from student

【讨论】:

    【解决方案2】:

    我们在 sql 中有 switch。你可以使用它

    select student_id ,case  from table_name
    
     CASE prefered_cell 
        WHEN 'cell_1' THEN 'value is cell_1 '
        WHEN 'cell_2' THEN 'value is cell_2'
        WHEN 'cell_3'= 'cell_3' then cell_3
      END
      else '0'
       End from student
    

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2020-08-18
      相关资源
      最近更新 更多