I am trying to limit the following SQL statement.
SELECT expense.*, transaction.* FROM expense
INNER JOIN transaction ON expense_id = transaction_expense_id
What I want to do, is limit the number of 'parent' rows. IE. if I do a LIMIT 1, I would receive only one expense item, but still get all transactions associated with it.
How would this be achieved?
At this stage, if I do LIMIT 1, I get one expense, and only one transaction.
Since upgrading the SQL server is not an option, I may end up doing two queries.
You'll have to specify which expense item you want to get. The most expensive? The newest? Then join against a subquery that returns only that:
So assuming we can exclude the user table, it could be rewritten as:
Now if you want to apply a limit, you could do it like this:
Would that do what you wanted? Obviously you need to be cautious about what order your expense_ids are going to come back in, so you probably want to use ORDER BY whatever.
Edit: Given the MySQL limitation described in your comment below, maybe this will work:
Ben