douren5490 2013-12-07 03:16
浏览 24
已采纳

如何从一个表中提取大量多行?

This is an expansion of my original question located here: How do I pull all rows from a table with one unique field and specific values for another field?

I have a table with two fields: user_id and skill_id.

I want to pull out all rows that have a skill_id of a certain number but I have a large number of skill_id's to search for (~30). I was using the self-join suggestion presented in the question linked above but with so many skills to look for, that query is proving extremely slow.

How can I look for a large number of skill_ids without bogging down the query?

EDIT:

Here's an example of what I'm looking for. Using the table below, I want to pull out all rows of users that have skill_id of 10 AND 11 AND 12, etc. (except I'd be looking for more like 30 skills at a time).

TABLE

user_id   |  skill_id
=====================
1         |    10
1         |    11
1         |    12
1         |    13
2         |    10
2         |    12
2         |    13
3         |    15
3         |    16
4         |    10
5         |    45
5         |    46
  • 写回答

2条回答 默认 最新

  • doubi2145 2013-12-07 03:26
    关注

    If I understand your question well, below query might help you. Assuming (user_id, skill_id) is UNIQUE or PK.

    SELECT user_id
    FROM tab
    WHERE skill_id IN (30 entries)
    GROUP BY user_id
    HAVING SUM(skill_id IN (30 entries)) = 30;
    

    You can test here. http://www.sqlfiddle.com/#!2/f73dfe/1/0

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

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)