简体   繁体   English

如何在MySQL上仅获得2个表的公共行?

[英]How to get ONLY common rows of 2 tables on MySQL?

Probably is something simple, but, let's say I have these: 可能很简单,但是,让我说这些:

Table User (id_user, name) 
Table A (id_a, name, type, #id_user)

And then I have another one that have only its own id and the other foreign keys 然后我有另一个只有自己的ID和其他外键的ID

Table B (id_b, #id_user1, #id_user2, #id_a, #id_Something)

So, I need a query that returns ONLY the rows of table A and table B with what they have in common. 因此,我需要一个查询,该查询仅返回表A和表B的行以及它们的共同点。 I've tried INNER JOIN but it returns all rows of Table A where the id_user from there is equal to the id_user from table B. Like, if I have these: 我尝试了INNER JOIN但是它返回表A的所有行,其中的id_user等于表B的id_user。例如,如果我拥有这些:

Table User: 表用户:

id_user   name
1         Hey

Table A: 表A:

id_a   name   type   id_user
1      a      car    1
2      b      cat    1

Table B: 表B:

id_b   id_user   id_user2         id_a   id_Something
1      1         Doesn't matter   1      Doesn't matter

I need to return only the common row between Table A and Table B (that'll be something like: 我只需要返回表A和表B之间的公共行(将是这样的:

id_a   name   type   id_user   id_b   id_user2
1      a      car    1         1

I've tried INNER JOIN but it returns to me everything when the id_user from A = id_user from B. I used this syntax: 我试过INNER JOIN但当它返回到了我一切id_user从A = id_user从B.我用这个语法:

SELECT     * 
FROM       B 
INNER JOIN A ON A.id_user = B.id_user;

Hope I've made myself clear, thank you a lot. 希望我已经说清楚了,非常感谢。

Is what you're going after: "Show me all the rows in A and B which share the same id_user" 这就是您要执行的操作:“显示A和B中共享相同id_user的所有行”

SELECT User.id_user, User.name, a.id_a, b.id_b
FROM User
INNER JOIN A ON a.id_user = User.id_user
INNER JOIN B on b.id_user = User.id_user

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM