SELECT in 2 Mysql tables with PHP

Asked

Viewed 141 times

1

I have a question when giving a select, because I have 3 tables, they are professionals, experiences, address.

Let’s say I have a professional and this professional has more than 2 experiences. There is some way to bring on a line this information, if not how can I do it in php, so that it does not repeat the name of the professional.

Tables:

Professional:

id

name

surname

civil state

experiences:

id

office

dt_input

dt_output

id_professional

My select for now is so

SELECT pro.id, pro.nome, exp.cargo
FROM Profissional pro
INNER JOIN experiencias exp ON exp.id_profissional = pro.id
WHERE pro.id = :algumacoisa GROUP BY pro.id

Example of an exit

We have 1 professional with 2 registered positions.

ID             Nome                  Cargo           Cargo

1             romario            programador        Técnico

I know the table name doesn’t repeat itself, but if you have a solution, I’d appreciate it.

  • 1

    Tried to use Inner Join?

  • You’ll need to use both Mysql and PHP to do this. Post table structures to help you.

  • Okay, I was able to illustrate a little more my doubt.

2 answers

3


Guys, I got it figured out, All I had to do was use GROUP_CONCAT();

Stayed like this:

SELECT pro.id, pro.nome, GROUP_CONCAT(exp.cargo)
FROM Profissional pro
INNER JOIN experiencias exp ON exp.id_profissional = pro.id
WHERE pro.id = :algumacoisa GROUP BY pro.id

It brings all the positions of this user in a single line without repeating the name and separated with comma.

0

SELECT pro.id,pro.nome,exp.id,exp.nome
   FROM Profissional pro, experiencias exp 
WHERE exp.id = pro.id and pro.id = :algumacoisa
  • Of that I already know, he repeated the name to show more of an experience.

  • give a look here http://stackoverflow.com/questions/15603650/mysql-2-select-in-one-query

  • 1

    You haven’t cleared my doubt yet!

  • 1

    The ideal is to use John to "merge" the tables and show everything in one.

Browser other questions tagged

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