-1

after hours of trying I thought I ask for your help here. My problem is the following:

I have two tables. One table contains general event data and the second table contains single events that refer to the general table (there can be several single events refering to one general event row).

The structure is as following:

1. General events table "events":

id | club_id | date_created | description | imageLink | ....

2. Single events table: "events_single"

id | events_id (id from events table) | valid_from | valid_until

All timestamps are unix timestamps

So what I want to get is the nearest single event in the future for a club with a certain club_id. The single event row shall be joined with the corresponding event row.

Form the following link I managed to get the nearest single events for all clubs : MySQL order by before group by

The query looks like this:

SELECT p1 . *
FROM events_single p1
INNER JOIN (
    SELECT min( valid_from ) AS firstEvent, events_id
    FROM events_single
    WHERE events_single.valid_from >= UNIX_TIMESTAMP()
    GROUP BY events_id
)p3 ON p1.events_id = p3.events_id
    AND p1.valid_from = p3.firstEvent
    ORDER BY p1.valid_from ASC

This returns for example:

id  |events_id  |valid_from|valid_until
4   |1          |1446706800|1446793200
39  |7          |1446966000|1447052400
14  |4          |1447311600|1447398000

The problem is that I now need to join this table again with the events table to get only the single events for a certain club.

E.g. if the general events table is

id  | club_id   | date_created  | ....
1   | 1         | 1446793200    | ....   <------            
2   | 2         | 1456793235    | ....              
3   | 5         | 1458735234    | ....
4   | 1         | 1458783425    | ....   <------    
5   | 2         | 1458953256    | ....
6   | 4         | 1461983283    | ....
7   | 5         | 1461993452    | ....

and I want to get now all single events for club_id = 1 and should return both tables joined:

id  |events_id  |valid_from|valid_until|club_id     | date_created  | ....
4   |1          |1446706800|1446793200 |1           | 1446793200    | ....  
14  |4          |1447311600|1447398000 |1           | 1458783425    | ....

I tried the following query:

SELECT p1.*, p2.*
FROM events_single p1, events p2
INNER JOIN (
    SELECT min( valid_from ) AS firstEvent, events_id
    FROM events_single
    WHERE events_single.valid_from >= UNIX_TIMESTAMP()
    GROUP BY events_id
)p3 ON p1.events_id = p3.events_id
    AND p1.valid_from = p3.firstEvent
    WHERE p2.club_id = 1
    ORDER BY p1.valid_from ASC

but it I get an error:

#1054 - Unknown column 'p1.events_id' in 'on clause' 

If I replace 'p1.events_id' with 'events_single.events_id' I get the same error.

Why is that? Or the better question: What's the correct query?

Thanks a lot in advance!

Community
  • 1
  • 1
S. F.
  • 206
  • 4
  • 14
  • 1
    try this:`SELECT p1.*, p2.* FROM events_single p1 inner join events p2 ON p1.events_id = p2.id WHERE p2.club_id = 1 and p1.valid_from >= UNIX_TIMESTAMP() ORDER BY p1.valid_from ASC` – Suchit kumar Oct 31 '15 at 09:52
  • why do you need `min( valid_from )`. – Suchit kumar Oct 31 '15 at 09:53
  • 1
    *From the following link I managed to get the nearest single events for all clubs* Um, no you didn't!??! – Strawberry Oct 31 '15 at 09:58
  • @Suchit , Probably event_single table contains lot of more than one entry for single event_id, that's why I think he needs min(valid_from) . Please correct me if I am wrong. – Shankar Oct 31 '15 at 10:15
  • @Suchit: thanks for your comment. Shankar is correct, there are several single events that are linked to one general event. Your query returns all single events, that are in the future. However I only need the first one which is closest to today – S. F. Oct 31 '15 at 10:42
  • then add `having min( p1.valid_from )` in the sql. – Suchit kumar Oct 31 '15 at 10:44
  • @Suchit, this unfortunately would only return one general event. However one club can have several general events and each of them can have several single events. However the suggestion from Shankar below worked! – S. F. Oct 31 '15 at 12:26
  • @S.F. Please accept my answer if it's working fine :-D – Shankar Oct 31 '15 at 12:55

1 Answers1

1

It seems you need to again join with Events Table again. In some other ways alias p1 is not visible in the ON Clause , as you again added events p2. Please try this query as below which will fix your issue.

SELECT p1.*, p2.*
FROM events_single p1
INNER JOIN (
    SELECT min( valid_from ) AS firstEvent, events_id
    FROM events_single
    WHERE events_single.valid_from >= UNIX_TIMESTAMP()
    GROUP BY events_id
)p3 ON (p1.events_id = p3.events_id
    AND p1.valid_from = p3.firstEvent)
INNER JOIN 
events p2 ON (p2.id = p3.event_id)
    WHERE p2.club_id = 1
    ORDER BY p1.valid_from ASC

We can optimize the above query as follows

SELECT events.*, es_tmp.*
    FROM events
    INNER JOIN (
        SELECT min( valid_from ) AS firstEvent, events_id
        FROM events_single
        WHERE events_single.valid_from >= UNIX_TIMESTAMP()
        GROUP BY events_id
    )es_tmp ON (events.id = es_tmp.events_id)
    WHERE events.club_id = 1
        ORDER BY es_tmp.firstEvent ASC
Shankar
  • 846
  • 8
  • 24
  • Hi Shankar, thank you so much for your effort! Unfortunately your queries don't work. The first query returns that there is an SQL error at the last part "INNER JOIN events p2 (p2.id....). The second query says that in the last line of code ORDER BY es_tmp.valid_from "es_tmp.valid_from" is unknown. – S. F. Oct 31 '15 at 10:46
  • Hi S.F I didn't test the query, as I don't create the schema . Will update the queries after creating similar structure, Could you please share if you your schema in http://sqlfiddle.com/ – Shankar Oct 31 '15 at 10:49
  • @S.F I have updated the second query please try that again in you schema once ? Could you please post the entire error, it would be easy for me to debug? – Shankar Oct 31 '15 at 10:50
  • @S.F. I updated both queries, both of them having syntax issue. I fixed it , could you please check now ? – Shankar Oct 31 '15 at 10:57
  • a million thanks! After your changes of the second query it ran without an error. However it showed the last single event not the first one. after I removed the first valid_from in "INNER JOIN ( SELECT valid_from,min( valid_from ) AS firstEvent, events_id" it worked as expected! Could you please change that in your answer to "INNER JOIN ( SELECT min( valid_from ) AS firstEvent, events_id" then I will accept it as correct :) Thanks again! You saved my weekend! – S. F. Oct 31 '15 at 12:24
  • btw, the first query is also working after your last change! Thanks, now I understand how joins are working :-) – S. F. Oct 31 '15 at 12:45
  • @S.F. You can accept my answer if it is working fine :-D – Shankar Oct 31 '15 at 12:50
  • yes sure. But as I mentioned in two comments before, could you please change ""INNER JOIN ( SELECT valid_from,min( valid_from ) AS firstEvent, events_id" in your answer to "INNER JOIN ( SELECT min( valid_from ) AS firstEvent, events_id" – S. F. Oct 31 '15 at 13:01
  • Thanks S.F. I missed your last comment , I removed the column name which is not needed as per your comment . – Shankar Oct 31 '15 at 13:06
  • ok the first solution is ok now, but the second (optimized) solution still has it in there. – S. F. Oct 31 '15 at 13:13
  • 1
    @S.F. I updated on my Second Solution (optimized )only as per your comment. – Shankar Oct 31 '15 at 13:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93869/discussion-between-shankar-and-s-f). – Shankar Oct 31 '15 at 13:33