dongshiran7000 2012-08-28 02:21
浏览 30
已采纳

在SQL字段中有一个数组。 如何系统地显示它?

I have a field in a data feed coming in with some values separated by commas. For one record, the values are:

A06,C05,C06,C15,C18,C19,C21,C22,E05,E22,G11,J02,J07,L04,L07,M01,M05,N03,N07,N10,N11,N12,N18,N19,N20,N24,O02,O03,O04,O06,O09,O14,O15,O16,O20,O21,O31,Q01,Q04,Q08,R07,S08,T08,T12,T23,T32,U01,U03,U04,U06,U13,W09,W11,W16,W19,W30,W45,X02,X03,X12,Z07

I have a separate table with some descriptions as to what each code means. When I query the main table and get this field name as a value, I can use explode to get it into an array and use a foreach loop to output each value.

The problem is, I want to display the description stored in another table. What's the proper way of iterating through this to display these values in a list?

As an example, C21 means "Gated Community."

  • 写回答

3条回答 默认 最新

  • doy57007 2012-08-28 02:24
    关注

    You can use FIND_IN_SET() function for that.

    Example you have record like this

    Orders Table
    ------------------------------------
    OrderID     |     attachedCompanyIDs
    ------------------------------------
       1                     1,2,3               -- comma separated values
       2                     2,4     
    

    and

    Company Table
    --------------------------------------
    CompanyID      |        name
    --------------------------------------
        1                 Company 1
        2                 Another Company
        3                 StackOverflow
        4                 Nothing
    

    Using the function

    SELECT name 
    FROM orders, company
    WHERE orderID = 1 AND FIND_IN_SET(companyID, attachedCompanyIDs)
    

    will result

    name
    ---------------
    Company 1
    Another Company
    StackOverflow
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功