Relation between 3 mysql tables

Asked

Viewed 569 times

2

I am trying to make a relationship between three tables. I have the table tbl_eventos which will be a table where you will store the event and its date. The table tbl_servicos_extra will save extra services that can be added to the event. And finally the table tbl_servicos_extra_eventos, this table will make the relationship between the events and the extra services (many to many).

What happens now, is that when I associate an extra service to the event, I have to pick up all the extra services that are not being used on that day of the event. I mean, I have an event date, so I have to make an appointment for a <select> that has all the extra services that can be added but cannot be extra services that are already being used on the same day.

-tbl_eventos-
id_evento
data

-tbl_servicos_extra-
id_servico_extra
designacao

-tbl_servicos_extra_eventos-
id
id_evento
id_servico_extra

I am using mysql and php. To display the results I have the following:

        <select name="id_servico_extra">
        <?php
            $sql = mysql_query("SELECT * FROM tbl_servicos_extra");
            while($row = mysql_fetch_array($sql))
            {
        ?>
                <option value="<?php echo $row['id_servico_extra'];?>"><?php echo $row['designacao'];?></option>
        <?php
            }
        ?>
        </select>

Here there is no filtering to show the extra services that are being used on the same day by another event.

1 answer

3


select *
  from tbl_servicos_extra se
 where not exists (select 1
                     from tbl_servicos_extra_eventos see
                     join tbl_eventos ev
                       on ev.id_evento = see.id_evento
                    where see.id_servico_extra = se.id_servico_extra
                      and ev.data = :param_data)

Where has the :param_data you replace by the date you want to filter.

  • That was it. Thank you very much!

Browser other questions tagged

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