I have a nested SQL query :
SELECT *
FROM
(
SELECT *
FROM asset_status
ORDER BY session_id DESC
) tmp
GROUP BY asset_id, workflow_element_id
I would like to create a view from this query but MySQL doesn’t seem to allow subqueries in views. How can convert this to a join?
SQL Server does allow sub-queries in views. What you can’t do, is
SELECT *andGROUP BY a, bHave you tried… (I’ll assume this isn’t your whole query so I’ll make the minimum possible changes)
Also, note that the ORDER BY in the inner query is innefective (and possibly even dis-allowed), as the outer query is then allowed to re-order it (it won’t always come back in a different order, but this layout doesn’t guarnatee the order you seem to want). Even in the outer query, it may cause your results to be order when using the view, but again the optimiser is allowed to re-order the results. Unless the ORDER BY is in the query using the view, the order is never absolutely guaranteed…
Finally, you tagged this as a LEFT JOIN question. If you have a more complete example of the code, I’m sure someone will suggest an alternative layout. But I’m off out for a few days now. Good luck! 🙂