Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the first line for each ID
    primarykey
    data
    text
    <p>Today, I would like to get the list of all events of a list which have been selected or have the farest date. I actually use the following code </p> <pre><code>SELECT choices.id AS id, events.id AS event_id, events.owner_id, choices.selected, choices_dates.label AS events_date FROM events LEFT JOIN polls ON (events.id = polls.event_id) LEFT JOIN choices ON (polls.id = choices.poll_id) LEFT JOIN choices_dates ON (choices.id = choices_dates.choice_id) WHERE polls.kind = 'date' ORDER BY events.id, choices.selected DESC, events_date DESC; </code></pre> <p>I obtain this result :</p> <pre><code>id event_id owner_id selected events_date 2 1 1 0 2011-04-20 12:00:00 1 1 1 0 2011-04-20 00:00:00 7 2 1 0 2011-04-20 12:00:00 6 2 1 0 2011-04-20 00:00:00 13 3 1 0 2011-04-22 15:57:54 12 3 1 0 2011-04-20 12:00:00 11 3 1 0 2011-04-20 00:00:00 </code></pre> <p>But here is what I want :</p> <pre><code>id event_id owner_id selected events_date 2 1 1 0 2011-04-20 12:00:00 7 2 1 0 2011-04-20 12:00:00 13 3 1 0 2011-04-22 15:57:54 </code></pre> <p>I can't do nested query because I am creating a VIEW and the group by gives me random results (I obtain the first line for the event_id 1, and the last one for the event_id 2, ...)</p> <p>For now, I could do the job with php function, but it is not optimisez and my code is dirty.</p> <p>Does someone have any idea for doing this with a great SQL function ?</p> <p>Thanks, Kevin</p> <p>EDIT : I've successeed to do what i want : I've used two different VIEW :</p> <p>CREATE VIEWview_events_maxAS SELECT events.id AS event_id, MAX(choices.selected) AS max_selected, MAX(choices_dates.label) AS max_events_date FROM events LEFT JOIN polls ON (events.id = polls.event_id) LEFT JOIN choices ON (polls.id = choices.poll_id) LEFT JOIN choices_dates ON (choices.id = choices_dates.choice_id) WHERE polls.kind = 'date' GROUP BY event_id</p> <p>AND</p> <p>CREATE VIEW view_events_correct AS ( SELECT choices.id AS id, events.id AS event_id, events.owner_id, choices.selected, choices_dates.label AS event_date FROM events LEFT JOIN polls ON (events.id = polls.event_id) LEFT JOIN choices ON (polls.id = choices.poll_id) LEFT JOIN choices_dates ON (choices.id = choices_dates.choice_id INNER JOIN view_events_max ON (events.id = view_events_max.event_id AND (choices.selected = 1 OR (choices.selected = view_events_max.max_selected AND choices_dates.label = view_events_max.max_events_date))) )</p> <p>Thanks everybody for your help, Kevin</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload