How to select two Columns with Different values when the ID Matches

Asked

Viewed 51 times

0

Let’s Say I have 1 table (1) with the users info. A table 2 with users plans. I need to select each users plan and show on the same line in case he’s got more than 1 plan.

TABLE 1:

+------+------+
|ID    |NAME  |
+------+------+
|1     |A     |
+------+------+
|2     |B     |
+------+------+
|3     |C     |
+------+------+

TABLE 2:

+------+------+
|ID    |PLAN  |
+------+------+
|1     |ASUS  |
+------+------+
|2     |APPLE |
+------+------+
|2     |ASUS  |
+------+------+
|2     |ACER  |
+------+------+
|3     |ACER  |
+------+------+

The result should be:

+------+------+------+------+------+
|ID    |NAME  |PLAN  |PLAN  |PLAN  |
+------+------+------+------+------+
|1     |A     |ASUS  |null  |null  |
+------+------+------+------+------+
|2     |B     |APPLE |ASUS  |ACER  |
+------+------+------+------+------+
|3     |C     |ACER  |null  |null  |
+------+------+------+------+------+
  • 3

    Welcome to the Stackoverflow in Portuguese. As the name suggests, the official language used here is Portuguese. So, could you please translate your question? If you prefer, you can also ask the same question on Stackoverflow website in English.

1 answer

1

You can use INNER JOIN to join information from two or more tables (if you are talking about SQL).

Example:

SELECT a.*, b.* FROM 'table1' AS a INNER JOIN 'table2' AS b WHERE a.id = b.id;
  • 2

    No [en.so] it would be interesting for you to reply in Portuguese

  • 1

    Welcome to the Stackoverflow in Portuguese. As the name suggests, the official language used here is Portuguese. So, could you please translate your answer?

  • You can use INNER JOIN to join information from two or more tables (if you are talking about SQL). Example: SELECT a., b. FROM 'table1' AS a INNER JOIN 'table2' AS b WHERE a.id = b.id;

  • 3

    Hi Lucas, you can always click on [Edit]. I did it this time, but you’ll know how to do it next time :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.