I have the following statement:
SELECT s.*, u.`fname`, u.`lname`, aif.`airport`
FROM services s
INNER JOIN airports_in_flight aif
ON s.`id` = aif.`service_id`
INNER JOIN users u
ON s.`service_provider_id` = u.`id`
Lets say for example that this is the result I am getting:
id | ----- | ------ | ...............
1 | Jack | Jones | ...............
1 | Moses| Cohen | ...............
2 | Tom | Jones | ...............
3 | Luke | SkyWal | ...............
So I need to use LIMIT 2 so that I will get the first 3 rows (and not only the first 2). So the Limit should be based on the id column. It should work also when using OFFSET.
How can this be done?
Instead of reading the services table directly, use a sub query and join to that.
Depending on your version of mysql, you can't have a subquery IN using limit and offset (This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery') but this will still work.
EDIT:
If you set the inner query to this:
Then it will only return services with airports_in_flight and users (possibly add a distinct clause).
EDIT to clarify:
Right now you have this as your select:
You want to limit to 2 services (I don't know if you want all services, I'm guessing just the ones with matching users and airports in flight), so you need to put the limit on the right table.
In this case, the right table is the services table. To make things simple lets start with a simplified version of what you have:
I'm going to make an assumption that there is an airport in flight row for every service (we can add that complexity later).
To list the first 2 services, we want the limit on the services, not the whole query, so it would be:
Notice I replaced the table services with a query, that I can now limit, this is the subquery. If we now want to only look at services that have an airport in flight, we need to change that subquery from:
to
I've renamed the services and airports_in_flight tables ss and aifs so they don't collide with the names in the main query, put an inner join in to limit my rows to only service table, and limited by 2, so putting the subquery into the query we now get:
You should be able to then expand the subquery to add the inner join for users (thus limiting services to only those that have airports_in_flight rows and users rows), and add the users table to the main query.