mysqlsqljoinmariadbnatural-join

Mysql Complicated Natural Join Select Statement


I am trying to craft a natural join on two tables with a particular order and a particular range.

My tables

dictionary
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| wid   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| word  | varchar(56)      | NO   | UNI | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

g219
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| pos   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| wid   | int(10) unsigned | NO   | MUL | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

dictionary has a set a dictionary of unique (case-sensitive) words and tags with an id number assigned to each. g219 is essentially a book, where pos is the position of the word/tag in the book and wid is the corresponding word id from the dictionary. I can perform a complete natural join to lay out the book as follows:

SELECT word FROM dictionary
    NATURAL JOIN g219
    ORDER BY g219.pos;

I can also apply a limit by adding LIMIT [n], but this only returns the first [N] results. I do not know how to limit this to a specified range. Both

SELECT word FROM dictionary
    NATURAL JOIN g219
    ORDER BY g219.pos
    WHERE pos BETWEEN 50 AND 100;

and

SELECT word FROM dictionary
     NATURAL JOIN g219
     ORDER BY g219.pos
     WHERE pos > 50 AND pos < 100;

fail.


Solution

  • First "WHERE", then "ORDER BY" ;)