Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Query to find number of linked foreign key objects?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
James Masters  
View profile  
 More options Feb 28 2012, 2:04 am
From: James Masters <ja...@mastersgames.com>
Date: Mon, 27 Feb 2012 23:04:14 -0800 (PST)
Local: Tues, Feb 28 2012 2:04 am
Subject: Query to find number of linked foreign key objects?
Is it possible to construct a query that will find any records that
have at least 5 objects linked to it via a specific foreign key?  e.g.
in an address database, I might have a person table with a foreign key
to a phone table. So multiple phone records link to a person record.
Using only a Rose query, can I obtain all persons with 3 or more phone
records?

many thanks for any advice.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Perrin Harkins  
View profile  
 More options Feb 28 2012, 8:51 am
From: Perrin Harkins <per...@elem.com>
Date: Tue, 28 Feb 2012 08:51:28 -0500
Local: Tues, Feb 28 2012 8:51 am
Subject: Re: Query to find number of linked foreign key objects?

On Tue, Feb 28, 2012 at 2:04 AM, James Masters <ja...@mastersgames.com> wrote:
> Is it possible to construct a query that will find any records that
> have at least 5 objects linked to it via a specific foreign key?

Sure, but you'll either need to use GROUP BY...HAVING or a derived
table (subquery).  Try get_objects_from_sql().

- Perrin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jfrm  
View profile  
 More options Mar 5 2012, 4:33 am
From: jfrm <ja...@mastersgames.com>
Date: Mon, 5 Mar 2012 01:33:08 -0800 (PST)
Local: Mon, Mar 5 2012 4:33 am
Subject: Re: Query to find number of linked foreign key objects?

> This is helpful thank you.  Now I have managed to construct an SQL query
> that works e.g.:

SELECT ... FROM persons,phones WHERE persons.uid = phones.personuid GROUP
BY person.uid HAVING count(person.uid) > 5;

But I can't see how to convert this to a Rose::HTML query. I've got as far
as:

get_persons(query => [], with_objects => ['phones'], group_by =>
'person.uid')

but I can't see HAVING as an option in the Rose query documentation nor is
it clear to me how I can incorporate the SQL function COUNT into the query.
 Is this possible?

Or should I go to plan B and just use direct SQL in the rose query?  Or is
this hopeless and I should go to plan C (presumably less efficient) and
grab all the customers and then use Perl to narrow them down?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jfrm  
View profile  
 More options Mar 5 2012, 4:54 am
From: jfrm <ja...@mastersgames.com>
Date: Mon, 5 Mar 2012 01:54:23 -0800 (PST)
Local: Mon, Mar 5 2012 4:54 am
Subject: Re: Query to find number of linked foreign key objects?

> Now I've found the answer. For posterity, as the manual clearly says the
> GROUP BY method should be 'fully formed SQL'. So the following GROUP BY
> argument worked: 't1.uid having count(t1.uid) > 5'

I am left with a niggling problem though.  In the subsequent report, one of
the columns shown is 'Number of phone numbers' for which I was using
@{$person->phones}.  This now fails and gives a count of 1 - presumably
only one row is returned by rose so only 1 order is thought to exist.
 Before I used GROUP BY, it worked fine. In SQL if I use
COUNT('persons.uid'), the correct value is shown.  So unfortunately, I am
back to doing the thing in PERL unless anyone knows a way to fix this
problem?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Siracusa  
View profile  
 More options Mar 5 2012, 9:56 am
From: John Siracusa <sirac...@gmail.com>
Date: Mon, 5 Mar 2012 09:56:02 -0500
Local: Mon, Mar 5 2012 9:56 am
Subject: Re: Query to find number of linked foreign key objects?

On Mon, Mar 5, 2012 at 4:54 AM, jfrm <ja...@mastersgames.com> wrote:
>> Now I've found the answer. For posterity, as the manual clearly says the
>> GROUP BY method should be 'fully formed SQL'. So the following GROUP BY
>> argument worked: 't1.uid having count(t1.uid) > 5'

You shouldn't try to use "group by" with the Manager's get_objects()
method (or anything derived from it, like get_persons()).  It expects
to create a tree of RDBO objects from the results, one object for each
uniquely identified row in a table.  Once you use "group by," you're
definitely not going to get result rows like that.  I suggest making a
Manager method that uses get_objects_sql() to build the WHERE part of
the query, builds the rest of the query "manually," runs it, then
builds and returns whatever result objects or data structures are
appropriate.

-John


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »