选择具有多个相同项的位置

发布于 2024-10-04 21:31:10 字数 473 浏览 4 评论 0原文

我有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迟月 2024-10-11 21:31:10

这可能适用于所有情况,也可能不适用于所有情况,但我尝试使用 (1,2,3) 和 (1,7):

select distinct letter_id
from r r1
    inner join r r2 using (letter_id)
where r1.number_id in (1, 7)
    and r2.number_id in (1,7)
    and (r1.number_id  r2.number_id);

您必须能够提供 (1,7)或使用某种编程语言动态地 (1,2,3)

凌晨 3:30 摇晃婴儿...

编辑:要完成@Martin 的答案,您可以使用 order by field()

select letter_id 
from (
    select letter_id,
        group_concat(number_id order by field(number_id,2,1,3)) as numset
    from r 
    group by letter_id
    ) as Martin
where numset = '2,1,3';

This may or may not work in all cases, but I tried with (1,2,3) and (1,7):

select distinct letter_id
from r r1
    inner join r r2 using (letter_id)
where r1.number_id in (1, 7)
    and r2.number_id in (1,7)
    and (r1.number_id  r2.number_id);

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()

select letter_id 
from (
    select letter_id,
        group_concat(number_id order by field(number_id,2,1,3)) as numset
    from r 
    group by letter_id
    ) as Martin
where numset = '2,1,3';
云朵有点甜 2024-10-11 21:31:10

如果您可以从 number_ids 列表构造一个字符串,则可以使用以下查询:

select letter_id 
from (select letter_id, group_concat(number_id) as numset from `matches` 
      group by letter_id) as fred 
where numset = '1,2,3';

它对顺序敏感(例如“2,1,3”将不匹配)。

If you can construct a string from your list of number_ids, you could use the following query:

select letter_id 
from (select letter_id, group_concat(number_id) as numset from `matches` 
      group by letter_id) as fred 
where numset = '1,2,3';

It is sensitive to order (eg. '2,1,3' would not match).

魔法少女 2024-10-11 21:31:10

由于之前的评论使问题超出了OP的范围,所以这是另一个答案...

您可以通过使用临时表来解决这个问题:

create temporary table r_sum as
    select letter_id, count(*) as total
    from r 
    group by letter_id;
create temporary table r_count as
    select letter_id, count(*) as total
    from r
    where number_id in (1,2,3,7)
    group by letter_id;
select letter_id
from r_sum
    inner join r_count using (letter_id, total);

我认为如果这不能回答您的问题,我就没有得到您想要的以及如何帮助您。如果表很大,您将必须在临时表上创建索引以帮助加快速度。 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:

create temporary table r_sum as
    select letter_id, count(*) as total
    from r 
    group by letter_id;
create temporary table r_count as
    select letter_id, count(*) as total
    from r
    where number_id in (1,2,3,7)
    group by letter_id;
select letter_id
from r_sum
    inner join r_count using (letter_id, total);

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.

辞取 2024-10-11 21:31:10

您必须为此使用 IN 语句。

尝试下面的查询。

SELECT letter_id WHERE number_id IN (1,2,3)

如果有的话,您可以将数组变量传递到 IN 语句中。

谢谢。

you have to use IN statement for that.

try below query for that.

SELECT letter_id WHERE number_id IN (1,2,3)

you can pass array variable into the IN statement if you have.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文