Mysql 如何根据第一个表的列值从两个表中获取行信息

Mysql 如何根据第一个表的列值从两个表中获取行信息,mysql,Mysql,我有以下两个表格: 表 article_id | attribute_id 1 | 5 2 | 6 表B attribute_id | attribute_name 5 | foo 6 | bar 如果我只知道文章id,如何获得相应的行?因此,如果我通过第1条,我会得到: article_id | attribute_id | attribute_name 1 | 5 |

我有以下两个表格:

article_id | attribute_id
1          | 5
2          | 6
表B

attribute_id | attribute_name
5            | foo
6            | bar
如果我只知道文章id,如何获得相应的行?因此,如果我通过第1条,我会得到:

article_id | attribute_id | attribute_name
1          | 5            | foo
我知道我可以用两个单独的查询来完成,但我想知道是否可以用一个查询来完成?我曾考虑在上使用内部联接,但两个表中都没有article_id

谢谢

当然,您可以(而且必须)使用
内部连接

SELECT TableA.article_id, TableB.*
FROM TableA
INNER JOIN TableB ON TableA.attribute_id = TableB.attribute_id
WHERE TableA.article_id = 1
SELECT
部分让我们从第一个表中检索
article\u id
,并从第二个表中检索所有字段。
inuser JOIN
子句将两个表中具有相同
属性\u id
的行连接起来。
WHERE
条件允许我们仅选择第一个表中
article\u id=1
的行。

当然,您可以(并且必须)使用
内部联接

SELECT TableA.article_id, TableB.*
FROM TableA
INNER JOIN TableB ON TableA.attribute_id = TableB.attribute_id
WHERE TableA.article_id = 1
SELECT
部分让我们从第一个表中检索
article\u id
,并从第二个表中检索所有字段。
inuser JOIN
子句将两个表中具有相同
属性\u id
的行连接起来。

WHERE
条件允许我们仅选择第一个表中
article\u id=1
的行。

使用属性\u id连接

SELECT * FROM TableA A, TableB B where
A.attribute_id = B.attribute_id

使用属性_id连接

SELECT * FROM TableA A, TableB B where
A.attribute_id = B.attribute_id
使用自然连接-

使用自然连接-