0

I have an sql sentence like this:

$sql = "SELECT *, films.category AS filmCategory FROM films LEFT OUTER JOIN items ON items.unique = films.ref ORDER BY unique ASC LIMIT 0, 4";

that doesn't work. It works without the ORDER BY and LIMIT parts, like this:

$sql = "SELECT *, films.category AS filmCategory FROM films LEFT OUTER JOIN items ON items.unique = films.ref";

I can't seem to find the way to add ORDER BY and LIMIT to get only the first 4 items from the films table. The joined part is only supposed to get some further information from another table for these four items afterwards. How is it done in the right way?

4

1 回答 1

2

If you want to limit a specific table, you have to use a subquery.

SELECT *, films.category AS filmCategory
FROM (SELECT *
      FROM films
      ORDER BY ref ASC
      LIMIT 4) films
LEFT OUTER JOIN items
ON items.`unique` = films.ref
ORDER BY `unique` ASC
于 2013-06-18T21:47:30.123 回答