Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect data: paginating data from multiple tables with inconsistent dates
    primarykey
    data
    text
    <p>I have 8 separate tables. Each table has id, a datetime field and some text. </p> <p>I am presenting data combined from all tables on a single page in a timeline view, most recent entries are at the top, and entries from each table are mixed in this timeline.</p> <p>Now the hard part - I need to add pagination to this website, so on each page I want to show exactly 10 days (except for the last page, that may have less than 10). Each table may have variable number of rows.</p> <p>I've been struggling with this for quite some time but have not yet come up with an elegant solution.</p> <p>Here's an example (I'll pick only two tables for this example to make it simpler).</p> <h2>tableA</h2> <p>Apr 1 | a1<br> Apr 5 | a2<br> Apr 7 | a3</p> <h2>tableB</h2> <p>Apr 2 | b1<br> Apr 2 | b2<br> Apr 5 | b3<br> Apr 6 | b4 </p> <p>Global timeline would look like this</p> <hr> <p>Apr 7 a3<br> Apr 6 b4<br> Apr 5 a2<br> Apr 5 b3<br> Apr 2 b2<br> Apr 2 b1<br> Apr 1 a1 </p> <p>And if each page shows only 3 days, I need for it to look like this:</p> <p>--- p1 ---<br> Apr 7 a3<br> Apr 6 b4<br> Apr 5 a2 </p> <p>--- p2 ---<br> Apr 5 b3<br> Apr 2 b2<br> Apr 2 b1 </p> <p>--- p3 ---<br> Apr 1 a1 </p> <p>The problem is - I can't figure out a way to query for this data in an elegant way. Here's some live query that I've been messing with:</p> <p>select date(d.entryTime) entryDate, date(wc.changeTime) wcDate from diary_entry d join water_change wc on d.aquariumId = wc.aquariumId where d.aquariumId = 2 group by entryDate order by entryDate limit 10, 5</p> <p>so, for one table I have this query: select date(d.entryTime) entryDate from diary_entry d where d.aquariumId = 2 group by entryDate</p> <p>it yields 13 results entryDate 2012-01-13<br> 2012-01-14<br> 2012-01-25<br> 2012-01-26<br> 2012-01-31<br> 2012-02-04<br> 2012-02-17<br> 2012-02-20<br> 2012-02-25<br> 2012-03-17<br> 2012-03-31<br> 2012-04-01<br> 2012-04-06 </p> <p>and for another: select date(wc.changeTime) changeDate from water_change wc where wc.aquariumId = 2 group by changeTime</p> <p>it yields 8 results 2012-01-22<br> 2012-01-26<br> 2012-02-17<br> 2012-02-25<br> 2012-03-04<br> 2012-03-10<br> 2012-04-04<br> 2012-04-24 </p> <p>There are three common days between the two 2012-01-26 2012-02-17 2012-02-25</p> <p>So the query that I need to produce would have to have 13 + 8 - 3 rows = 18 rows</p> <p>And solution is found! (select date(d.entryTime) activityDate from diary_entry d where d.aquariumId = 1 group by activityDate) union (select date(wc.changeTime) activityDate from water_change wc where wc.aquariumId = 1 group by activityDate order by activityDate) limit 10, 10</p>
    singulars
    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