简体   繁体   English

选择表中ID等于另一个表中另一个ID的行

[英]Select rows in a table where id is equals to another id in another table

I would like to select a certain row in my getadvocacy table where id is the id in another table called swimsuit. 我想在我的getadvocacy表中选择某一行,其中id是另一个表中的id,称为Swimsuit。

swimsuit table 泳衣桌

id         |         name          |       average
1          |         Abc           |       90
3          |         Def           |       99

getadvocacy 宣传

id         |        all_scores     |       average_score
1          |        70,70,70       |       70
2          |        70,70,70       |       70
3          |        70,70,70       |       70

Now, I want to select from getadvocacy but only 1 and 3 because it is the data on swimsuit. 现在,我要从getadvocacy中进行选择,但只能选择1和3,因为它是泳衣上的数据。

Expected Output 预期产量

id         |        all_scores     |       average_score
1          |        70,70,70       |       70
3          |        70,70,70       |       70

I tried this but it has different output. 我试过了,但是它有不同的输出。

select getadvocacy.id, all_scores, average_score from getadvocacy WHERE getadvocacy.id IN (select id from swimsuit)

If the id (primary key) is same for table then you can use join on Id 如果表的ID(主键)相同,则可以在ID上使用join

 select * from table1
        JOIN table2 
             on table1.id = table2.id

Use this 用这个

  select * from swimsuit JOIN getadvocacy ON swimsuit.id= getadvocacy.id;

Result of query is 查询结果是

    1   abc 90  1   50,60,70    70
    3   def 99  3   60,70,70    70

in standard sql you have to do: 在标准sql中,您必须执行以下操作:

select * 
from getadvocacy
where
    id in (select st.id from "swimsuit table" as st)

Simplest and easier to understand: Just by reading between the lines, you will understand how this snippet works 最简单易懂:只需在两行之间阅读,您就会了解此代码段的工作方式

SELECT getadvocacy.* , swimsuittable.*
FROM getadvocacy, swimsuittable
WHERE getadvocacy.id = swimsuittable.id

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

相关问题 使用Cakephp选择ID不在另一个表中的所有行 - Select all rows where the id is not in another table using Cakephp 如何选择一个表中存在ID的行,而另一表中没有ID? - How to select rows where the ID exists in one table, but not another? 从ID不在另一个表中选择 - Select from table where id is not in another 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column MySql:从另一个表插入表SELECT中,其中first_table ID = another_table.id - MySql: insert into table SELECT from another table where first_table ID =another_table.id 从表中选择,其中 id 没有用另一个 user_id 表示 - SELECT from table where id is not represented with another user_id MySQL-如何在另一个表的逗号分隔字段中选择ID值所在的表中的行? - MySQL - How to select rows in a table where id value is in a comma delimited field in another table? 从表中选择ID在另一个表中的逗号分隔列表中的行 - Select rows from table where id is in a comma seperated list in another table 从存在ID(从另一表)的一个表中选择 - Select from one table where id (from another table) exists 从表中选择字段,其中id不在mysql的另一个表中[不起作用] - select fields from table where id not in another table in mysql [not working]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM