How to display 2 columns from 2 different tables in Mysql?

Asked

Viewed 3,989 times

4

I have 2 tables with multiple columns.

I would like you to display individual columns of more than one table at the same time:

tabela1 | tabela2

Nome    | Apelido
  • Can provide more details?

  • Are the two tables concactenated to each other? Do you have any common field?

  • This is quite simple. Do you have any special requirements? How are they related? I look forward to additional information to improve the response

  • It worked here with the commands that colleagues answered, it was just that :SELECT Tabela1.name, table2.nickname from Tabela1, table2 as Bigown answered. I’m learning to use mysql commands now. Thanks for the help.. As soon as I have more questions I will call them.

  • @David I saw that you tried to accept other answers too. Acceptance can only one. But soon you can vote on all the answers, or questions you want, even questions that aren’t yours. Votes help to rank the correct content when you can evaluate. The acceptance shows which answer you thought best answers what you wanted. See the [tour]. Welcome to the site, I think you’ll like it here.

  • is tried even, rs thanks. Half lost yet.

Show 1 more comment

4 answers

10


It’s not much of a secret:

SELECT tabela1.nome, tabela2.apelido FROM tabela1, tabela2;

I put in the Github for future reference.

After the SELECT you will place the list of columns you want to be shown. When you have more than one table it is important to qualify the column completely, that is, tell which table the column belongs to, in other words, give it a surname, so it is more obvious where this column comes from and avoid ambiguity if the tables have columns with the same name. In some cases the table name may be omitted but it is usually interesting to put anyway.

Then the clause FROM you list all the tables that should be available in the query. Without this you cannot use them for all operations. It is possible to create a nickname for the table name as shown in another answer but it does not come to the case for what you want. It’s just a facility when you will often use the table name.

2

Assuming you have a Table 1 with the following fields:

Tabela1
-Id - Integer
-Nomes - String
-IdTabela2 - Integer

and a Table2 with the following fields:

Tabela2
-Id - Integer
-Apelidio - String

run the following query:

SELECT t1.Nome, t2.Apelido
FROM Tabela1 AS t1, Tabela2 AS t2
WHERE t1.IdTabela2 == t2.id

in the third row WHERE t1.IdTabela2 == t2.id says that "where the column Idtable 2 of Table 1 is equal to the column ID of table 2", thus occurring the relationship between the two tables, ie query will display all names with their respective surnames that are related through the Foreign key IdTabela2, I hope you’ve made your point.

  • It would be interesting to explain his answer, so in addition to knowing how to do he will understand why

  • I edited, it looks better Erlon Charles?

  • Now it’s gotten much better, the more complete the answers the more they will help in future searches for being questions. Welcome :D

1

Simple as that:

SELECT
  `tabela1`.`Nome`,
  `tabela2`.`Apelido`
FROM
  `tabela1`,
  `tabela2`;

-1

And when you have a select using Inner Join and you want to scroll through multiple tables to display. Example:

id - razaosocial - common - opme 58 89

In this example "Common" is a select going through X tables and "Opme" is another select going through X tables, Both go through the tables that have the field of social razaosocial. With such an example I don’t know how to structure to show the information. I’ve tried using subselect and Union all, but it didn’t work. With Union all I can show the information but they stay in the common field and that’s not what I want to display column next to column.

Browser other questions tagged

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