doutuobao9736 2011-12-03 07:50
浏览 126

查询逗号分隔字符串的特定部分

I wonder if it is possible to query a specific part of a comma separated string, something like the following:

$results = mysql_query("SELECT * FROM table1 WHERE $pid=table1.recordA[2] ",$con);

$pid is a number

and recordA contains data like

34,9008,606,,416,2

where i want to check the third part (606)

Thank you in advance

  • 写回答

2条回答 默认 最新

  • dqrdlqpo775594 2011-12-03 09:12
    关注

    Having comma seperated lists or any data seperation within a mySQL field is frowned upon and is to all extents bad practice.

    Rather than looking at querying an element of a delimetered list within a mySQL field consider breaking the field into its own table and then creating an adjacency list to create a 1:many relationship between table1 and it's associated variables.

    If you are commited to this route, the simplest method would be to use PHP to manage it as mySQL has very few tools (above and beyond regex / text searches) to drill down to the data you want to extract. $results = explode(',',$query); would create an array of your variables from the returned field allowing you to run as many conditional checks against it as needed.

    However, consider adding this to your 'need to re-write / re-think' list. A relational tables structure would allow you to query the database for $pid's value directly as it would be contained within it's own field and linked

    If the delimetered variable list is of an inderterminate length or the relationships between the variables are heirarchical you'd be better off searching stackoverflow for information on Directed Acyclic Graphs in mySQL to find a better solution to the problem.

    Without knowing the nature or the intended purpose for this script I can't answer in any more detail. I hope this has helped a little.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法