Follows a possibility:
Create a table and indicate separated by records the id and possible treatments.
CREATE TABLE `table_agendamentos` (
`id_table_agendamentos` int(11) NOT NULL,
`id_agendamento` int(11) NOT NULL,
`id_tratamentos` int(11) NOT NULL,
PRIMARY KEY (`id_table_agendamentos`)
);
INSERT INTO `table_agendamentos` VALUES
(1,1,1),(2,2,1),(3,2,2),(4,2,3),(5,3,2),(6,3,3);
Keep the table below:
CREATE TABLE `tabe_tratamentos` (
`id_tabe_tratamentos` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(45) NOT NULL,
PRIMARY KEY (`id_tabe_tratamentos`)
);
INSERT INTO `tabe_tratamentos` VALUES (1,'Facial'),(2,'Corporal'),(3,'Outros');
Now to return what you want to do to the following SQL:
select * from tabe_tratamentos where id_tabe_tratamentos IN (
SELECT id_tratamentos FROM table_agendamentos where id_agendamento = 2)
or
Select TT.nome from tabe_tratamentos as TT
Inner join table_agendamentos as TA ON TA.id_tratamentos = TT.id_tabe_tratamentos
where TA.id_agendamento = 2;
All results:
Select TA.id_agendamento, GROUP_CONCAT(TT.nome) from tabe_tratamentos as TT
Inner join table_agendamentos as TA
ON TA.id_tratamentos = TT.id_tabe_tratamentos
GROUP BY TA.id_agendamento;
Just by eye I recommend normalizing properly to
table_agendamentos
. No problem repeating id if fields are composite keys.– gmsantos
If you don’t know what @gmsantos is talking about, check out an example at http://answall.com/a/33794/74
– bfavaretto