Saturday 15 September 2012

ruby on rails 3 - Finding records based on no returned records from a related table -


Handling two tables in postgrad: Members and subscriptions If membership is still going on, membership has an end_adet column Can be faucet As soon as it has expired, or the expiry date is known (future may be) the population of the field is dated.

Now I want to find all those members who do not have any active membership at this time (or: membership with only and_data and membership in previous or 0 memberships). Membership membership can be more than 1.

I am doing this in a Rail (3.2) project and I would like to be able to use ActiveRecord if possible, but if needed, directly SQL (although I know) ActiveWork is better ).

I have tried to find all kinds of things with sub-properties, but I can not find any way to select those rows where the number of specific rows in the corresponding table is 0.

I've been googling for some time, but how many ideas have gone out of search, to find the answers that most of those parameters are getting, and I think that they I do not match. Maybe it's a newbie question or I'm targeting it wrongly. In that case: Please highlight me, I'm starting a little bit.

Based on a partial answer by Jacob I have tried a lot. At the moment I have the following questions:

 SELECT m.id, m.first_name, m.last_name, COUNT (ms.id) MSN MEMBERSHIP member left M. Ms.member_id GROUP by M.ID, MS & Date Hoving (Max (MS & DDET) & lt; now () and no COUNT (MS & DATA = NULL) & gt; 0) or COUNT (MSID) = 0;  

This gives returns to all those members who have no subscriptions (no past and no current). Depending on the last part of the HAVING segment (or ... part) However, I hope that members and any current membership can not be withdrawn with the membership of the deadline, as maximum (MS & DD) will return less value from now and no subscription will be done without any expiration date. However, there is no idea?

Join what you are looking for is a OUTER There is no code for finding members with no subscriptions:

  Selection from Member M.ID = s.member_id as a member M. SAIID S is faucet;   

Full query:

  from Member SELECT m.id as Member M on current M.ID = s.member_id by GROUP. ID Having Max (S & amp; dd) & lt; Now () or maximum (sand_adet) IS tap is    

No comments:

Post a Comment