简体   繁体   English

如何从具有一个唯一字段和另一字段的特定值的表中提取所有行?

[英]How do I pull all rows from a table with one unique field and specific values for another field?

Here's what I'm looking to accomplish: 这是我想要实现的目标:

I have a table called "users_skills". 我有一个名为“ users_skills”的表。 The table consists of two fields, "user_id" and "skill_id". 该表由两个字段“ user_id”和“ skill_id”组成。 I want to pull all rows from the table that show a user has specific skills. 我想从表中拉出所有显示用户具有特定技能的行。

For example, I want to pull all users from "users_skills" who have skill_id 110, 120, and 130. If a user in "users_skills" doesn't have all 3, I don't want them in my results. 例如,我要从“ users_skills”中提取具有Skill_id 110、120和130的所有用户。如果“ users_skills”中的用户没有全部3个,则我不希望他们出现在结果中。

I've tried a simple AND (like users_skills.skill_id=110 AND users_skills.skill_id=120 AND users_skills.skill_id=130) but that didn't work. 我尝试了一个简单的AND(例如users_skills.skill_id = 110 AND users_skills.skill_id = 120 AND users_skills.skill_id = 130),但是没有用。

How would I accomplish this? 我将如何完成? Thanks! 谢谢!

Try using a self-join: 尝试使用自联接:

SELECT us1.* FROM users_skills us1
  JOIN users_skills us2 ON us1.user_id = us2.user_id
  JOIN users_skills us3 ON us1.user_id = us3.user_id
    WHERE us1.skill_id = 110 AND us2.skill_id = 120 AND us3.skill_id = 130

I assume you actually want the data from the tables users and skills , in that case, you could use 我假设您实际上是想要来自表usersskills的数据,在这种情况下,您可以使用

SELECT users.*, skills.* FROM users
  JOIN users_skills us1 ON users.id = user_id
  JOIN users_skills us2 ON users.id = us2.user_id
  JOIN users_skills us3 ON users.id = us3.user_id
  JOIN skills ON skills.id = us1.skill_id OR skills.id = us2.skill_id OR skills.id = us3.skill_id
    WHERE us1.skill_id = 110 AND us2.skill_id = 120 AND us3.skill_id = 130

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从MySQL表中提取两个具有相同字段值而另一字段不同的行? - How do I pull two rows from a MySQL table that have one field value in common and the other field different? 使用PHP和MySQL如何使用另一个表中的某些字段值填充一个表 - Using PHP and MySQL how do I populate one table using some field values from another table 如何使用公用字段将表中一个字段的值添加到另一表中另一个字段的值? - How to add values from one field in a table to another field in a different table using a common field? 如何选择另一个字段中具有特定值的字段的所有值? - How to select all values of a field that has specific values in another field? MySQL:如何从一列返回所有唯一值,同时匹配另一列中的值 - MySQL: How do I return all unique values from one column while matching values in another column 从另一个表中按唯一值分组的一个表中选择所有行 - Selecting all rows from one table grouped by unique values from another table 将一个表中字段的值用作另一表中字段的列名 - Using values from a field in one table as column name for field in another 如何将行从一个MySQL表复制到另一个表并基于变量更新值之一 - How do I copy rows from one MySQL table to another and update one of the values based on a variable 如果另一个表具有匹配的字段值,如何更新数据库表的1个字段或2个字段? - How do i update 1 field or 2 of a database table if another table has matching field values? 如何根据另一个表的ID字段从一个表获取数据? - How do i get data from one table according to an id field in another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM