Jump to content

Very difficult SQL-query


laurihasko
 Share

Recommended Posts

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 by laurihasko
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...