选择具有多个相同项的位置
我有一个名为“matches”的表,其中我通过 id 将表“numbers”中的 items 与表“letters”中的 item 关联起来。
所以看起来
number_id, letter_id
1,10
2,10
3,10
5,11
4,23
7,19
1,19
3,64
现在用户输入了一个数字数组,比如 1,2,3 我必须找到其中所有数字都是 1,2,3 的字母,即 10。如果他们给出 1,7,则会给出 19,如果他们给出 3,则会给出 64。
我怎样才能这样做吗?
我一直在尝试为此编写一个递归函数,但它总是失败。 有没有某种:
SELECT letter_id WHERE **number_id***s* = 1,2,3. That would be perfect. :)
I have a table called 'matches' where I associate a items in the table "numbers" with an item in the table "letters" via there id.
So it looks like
number_id, letter_id
1,10
2,10
3,10
5,11
4,23
7,19
1,19
3,64
now the user inputs an array of numbers, say 1,2,3
and I have to find the letter where all of it's numbers are 1,2,3, which would be 10. If they gave 1,7 it would give them 19, if they gave 3 it would give them and 64.
How can I do that?
I've been trying to write a recursive function for this but it always breaks.
is there some sort of:
SELECT letter_id WHERE **number_id***s* = 1,2,3. That would be perfect. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能适用于所有情况,也可能不适用于所有情况,但我尝试使用 (1,2,3) 和 (1,7):
您必须能够提供
(1,7)
或使用某种编程语言动态地(1,2,3)
。凌晨 3:30 摇晃婴儿...
编辑:要完成@Martin 的答案,您可以使用
order by field()
This may or may not work in all cases, but I tried with (1,2,3) and (1,7):
You'll have to be able to provide the
(1,7)
or(1,2,3)
dynamically with some programming language.Rocking baby at 3:30am...
EDIT: To complete the @Martin's answer, you can use
order by field()
如果您可以从 number_ids 列表构造一个字符串,则可以使用以下查询:
它对顺序敏感(例如“2,1,3”将不匹配)。
If you can construct a string from your list of number_ids, you could use the following query:
It is sensitive to order (eg. '2,1,3' would not match).
由于之前的评论使问题超出了OP的范围,所以这是另一个答案...
您可以通过使用临时表来解决这个问题:
我认为如果这不能回答您的问题,我就没有得到您想要的以及如何帮助您。如果表很大,您将必须在临时表上创建索引以帮助加快速度。
r
是您在 OP 中的原始表。Since the previous comments made the problem more than what the OP was, here's another answer...
You may be able to work it out by having temporary tables:
I think if this does not answer your question, I am not getting what you want and how to help you. If table is huge, you will have to create indexes on the temporary tables to help go faster.
r
is your original table in OP.您必须为此使用 IN 语句。
尝试下面的查询。
如果有的话,您可以将数组变量传递到 IN 语句中。
谢谢。
you have to use IN statement for that.
try below query for that.
you can pass array variable into the IN statement if you have.
Thanks.