I have the following two tables:
TableA
article_id | attribute_id
1 | 5
2 | 6
TableB
attribute_id | attribute_name
5 | foo
6 | bar
How would I get the corresponding row if I only know the article id? So if I pass article_id 1 I get:
article_id | attribute_id | attribute_name
1 | 5 | foo
I know I could do it with two separate queries but was wondering if it could be done in one? I thought about using INNER JOIN ON but article_id isn’t in both tables?
Thanks
For sure, you can (and have to) use
INNER JOIN.The
SELECTpart let us retrievearticle_idfrom the first table, and all fields from the second one.The
INNSER JOINclause joins rows that have the sameattribute_idfrom the two tables.The
WHEREcondition let us select only the row witharticle_id = 1in first table.