laurihasko Posted September 13, 2012 Share Posted September 13, 2012 (edited) Hey everyone this is my first post, sorry about the english. Q: Is it possible to connect these 2 queries in -> 1 query? TABLES (MySQL) event-E_Id reservation- Event_Id need- E_Id- G_Id- nNeed group- G_Id 1.) First I get all events and amount of reservations/ event 2.) Second I get need number for every event QUERY 1: SELECT event.*,COUNT(reservation.Event_Id)FROM eventLEFT JOIN reservation On Event_Id = E_IdWHERE eStatus = 'EVENTS'GROUP BY E_Id QUERY 2: SELECT SUM(need.nNeed)FROM needJOIN group ON need.G_Id = group.G_IdGROUP BY E_Id Thank you all, if this connection isn't possible, could you tell me that also. Edited September 15, 2012 by laurihasko Link to comment Share on other sites More sharing options...
justsomeguy Posted September 13, 2012 Share Posted September 13, 2012 You can use those queries as subqueries to create temporary tables that you can join. SELECT * FROM (SELECT event.*,COUNT(reservation.Event_Id)FROM eventLEFT JOIN reservation On Event_Id = E_IdWHERE eStatus = 'EVENTS'GROUP BY E_Id) AS tmp1 JOIN (SELECT E_Id, SUM(need.nNeed)FROM needJOIN group ON need.G_Id = group.G_IdGROUP BY E_Id) AS tmp2 ON tmp1.E_Id=tmp2.E_Id 1 Link to comment Share on other sites More sharing options...
niche Posted September 13, 2012 Share Posted September 13, 2012 Great post justsomeguy. Link to comment Share on other sites More sharing options...
laurihasko Posted September 15, 2012 Author Share Posted September 15, 2012 Thank you. I try to understand whole example. I am on level 1 or 2.. And if there is 10 levels.. Php levels, then I am beginner but I lean fast. Thanks a lot Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now