Gossamer Forum
Home : General : Databases and SQL :

Help with Perl SQL Statement

Quote Reply
Help with Perl SQL Statement
Hi could someone help out a non-perl programmer with this statement:

I want to set $num_cats = SELECT COUNT(Depth) FROM lsql_Category WHERE Depth=$count;

$count is from my for loop.

Here is what I have so far:

Code:


my ($num_cats) = $DB->table('Category')->select(['COUNT(Depth)'])->fetchrow_array;




Thanks!
Quote Reply
Re: [sooke] Help with Perl SQL Statement In reply to
I think I worked it out!

Is this right?

Code:


my ($num_cats) = $DB->table('Category')->select(['COUNT(Depth)'],{ 'Depth' => $count})->fetchrow_array;
I am assuming "WHERE" statements are just comma'd (blah, WHERE CONDITIONAL, WHERE CONDITIONAL)
Quote Reply
Re: [sooke] Help with Perl SQL Statement In reply to
>>Is this right? <<

Does it work?...if so then its right :)
Quote Reply
Re: [Paul] Help with Perl SQL Statement In reply to
"If it works its right"

Yes it does! ha ha.... unless its performance critical I guess, which does not matter in this case. I know there is usally better ways to write code.

I just worked out where to stick the 'WHERE' clause.... so I wanted to be sureWink

You know there is quite a bit of reference to beginers PERL on the net... but I can't find any good (simple) tutorials on Perl/SQL tutorials....
Quote Reply
Re: [sooke] Help with Perl SQL Statement In reply to
Really?

Have you tried GOOGLE?

http://www.google.com/...hl=en&q=Perl+SQL
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [sooke] Help with Perl SQL Statement In reply to
Hi,

Yes that's right.

You pass into select() a ARRAY Reference or list for what you want to select from, and a Hash Reference or GT::SQL::Condition ojbect for the where clause.

You don't want two hash references though (say you wanted Depth = 0 AND Root = 0), you do:

my ($num_cats) = $DB->table('Category')->select( 'COUNT(Depth)', { Depth => $count, Root => 0 })->fetchrow_array;

To do more complex where clauses, you'll need a Condition object. See the help page for GT::SQL::Condition.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Help with Perl SQL Statement In reply to
Thanks Alex, that is a big help!

I feel like I am starting to crawl a little with this nowSmile

I guess this is what you mean by a condition object:

Code:
$sth = $search_db->query_sth ( { Review_Validated => 'Yes' });


I am going to re-read that section in the help, now that I have learnt a few things.

Last edited by:

sooke: May 13, 2002, 9:17 AM
Quote Reply
Re: [sooke] Help with Perl SQL Statement In reply to
Nope, have a look at:

http://www.gossamer-threads.com/...T/SQL/Condition.html

(username admin, pass admin). You create the condition object, and then pass it into select.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Help with Perl SQL Statement In reply to
Ah huh! Thanks!!