MySQL SELECT 查询 - 一个单元格到多行
在 MySQL 中,我有一个特定的单元格,其中包含类似这样的数据。
5,6,7,8,9
如果我需要一个接一个地搜索特定的 2 个数字,我会使用 LIKE 语句 对这 2 个特定数字进行查询。对于前。我需要检查是否有一行包含数字 6 & 7 *连续*我做
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
这有点多余和笨拙。如果我将这些数字“转换”为多行,例如。每个数字都会成为它自己的行,其中“数字”列按“排序”列排序,这样我就知道行的顺序。
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
此案例的相同查询是什么?所以我会得到与上面查询相同的结果。我需要使用 sort 列对查询进行排序,并检查数字 6,7 是否在该排序中一个接一个地出现。
In MySQL I have one particular cell with data something like this
5,6,7,8,9
If I need to search for specific 2 numbers one after another I do a query with LIKE statement for those 2 particular numbers. For ex. I need to check if there's a row with numbers 6 & 7 *in a row* I do
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
It's little redundant and clumsy. If I 'convert' those numbers into multiple rows, for ex. every number would become it's own row with column 'numbers' ordered with 'sort' column so I know the order of rows.
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
What's the identical query for this case? So I would have the same result as with the query above. I need to order the query with sort column and check if the numbers 6,7 are occurring one after another with that sorting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该是这样的。 (如果我理解正确你的问题)。如果这 2 个数字没有按照排序列按顺序排列,则不会返回任何内容。
如果您不知道哪个应该是第一个,您可以这样使用它:
Should be something like this. (if I understood right your problem). It will return nothing if the 2 numbers are not in sequence by the sort column.
if you do not know which one should be first you can use it like this:
数字总是加一还是可以有任何值?
Are the numbery always incremented by one or can they have any value?
我提出以下表结构
I'm proposing the following table structure