I have guides which are linked to series (many to many) with a junction table like so:
guides
id etc.. (more columns)
--
1
13
21
30
series
id etc.. (more columns)
--
1
3
4
series_guides (primary key consists of these 3 columns) also guide_id is unique, a guide can only link to a series once.
PK series_id PK guide_id PK order
------------ -------- -----
1 13 1
1 21 3
1 1 2
3 30 2
Now my question is: How do I get the next guide when I know the ID of some other guide?
Example: Someone is reading guide 13 (guide_id), what query do I need to run to get the next guide from the series. Which is guide 1.
This is what I tried so far, and I actually think it works. However maybe there is a better way to do this?
SELECT
sg2.`order`,
sg2.guide_id
FROM series_guides sg
JOIN series_guides sg2 ON sg2.series_id = sg.series_id
WHERE sg.guide_id = 13 AND sg2.order > sg.`order`
ORDER BY sg2.`order`
LIMIT 1;
Any help is much appreciated
Best Answer
Your query is correct and does work. I think you can make the intent a little bit more clear by writing it in a slightly diffent way (although this is open for debate). The execution plans (on MariaDB) are exactly the same.
The comments in the middle should be explanatory:
This will give you:
If you change then the
guide_id
to be 1, you'll get the next value as:Check everything at dbfiddle here
Remarks and (simplified) table definitions:
I've defined the tables with different namings, to avoid confusions (and not to clash with the
ORDER
keyword, just in case):If
guide_id
must beUNIQUE
(andNOT NULL
, or otherwise, it doesn't make any sense) it's already acandidate key
. Being the simplestcandidate key
, it's the one I'd use asprimary key
:Probably, you would want to have also another restriction:
UNIQUE (guide_id, ord)
as well, to avoid situations where your order might not we well defined.