MySQL - Perl- Order by - How to display ABS(PartNum) DESC but, three different (PartNum) categories?
Sorry for my inaccurate syntax. New to this.
I need to select part numbers in three different groups.
A different qty for each group.
Each group individually sorted ABS desc.
my $fromrow = CGI::param('from');## vague but put here to show a sort of pagi开发者_高级运维nation ##
my $nextrow = CGI::param('next');
my $grabpartnumbers = $hookup->prepare(qq{
SELECT `New`,`Used`,`NOS`,`PartNum`,`Description`,`Price`
FROM `Partstable`
WHERE `instock` = ? AND `QTY` > ? AND `New` = ?
ORDER BY ABS(PartNum) LIMIT ?,?});
$grabpartnumbers->execute('1','10','1',$fromrow,$nextrow);
while(@row = $grabpartnumbers->fetchrow_array()) {
#do stuff
Works fine, for one "column" eg "New". However, I want to display like:
New Stock 4 results
NOS 6 results
Used Stock 10 results
Additional problems will arise for me when one of the three "columns" have no more data to display. At that point, I just want to display the remaining result or nothing (not even the title header) if results = 0 for that cat.
Is this possible or do I need to prepare three different queries and possibly push each into their own arrays to decipher what to display in some loops?
This is VERY advanced for me so, please be verbose with examples. (dumb it down to my level of comprehension (-; )
Thanks..
EDIT:
My Partstable rows look like
ID|PartNum|New|Used|NOS|Qty|instock|Description|Price
1|132452 |1 |0 |0 | 24|y |12ax7 yada |3.00
I want to display three "areas" on the same page by order of "importance". "New" take priority.
New Stock 4 results
NOS 6 results
Used Stock 10 results
What you need to achieve is a little bit difficult to precisely comprehend, but it looks like you could potentially be just fine with a full-SQL solution (clever use of GROUP BY
, etc.).
Regarding the current solution with Perl, and assuming I understand your example correctly, something like this would work as a starting point for you to play around and adapt to your actual dataset.
Warning: Code tested against syntax only!
my $fromrow = CGI::param('from');## vague but put here to show a sort of pagination ##
my $nextrow = CGI::param('next');
# Columns of interest, sorted by descending order of importance
my @interesting_columns = qw(
New
Used
NOS
PartNum
Description
Price
);
my $interesting_columns_expr = join(', ', map { $_ = "`$_`" } @interesting_columns);
my $grabpartnumbers = $hookup->prepare(qq{
SELECT $interesting_columns_expr
FROM `Partstable`
WHERE `instock` = ? AND `QTY` > ?
ORDER BY ABS(PartNum) LIMIT ?,?
});
$grabpartnumbers->execute('1','10',$fromrow,$nextrow);
# Compute results
my %result_counter;
while ( my $row = $grabpartnumbers->fetchrow_hashref() ) {
foreach my $column_name ( keys %$row ) {
$result_counter{$column_name} += $row->{$column_name};
}
}
# Display results
for my $column_name ( @interesting_columns ) {
if (exists $result_counter{$column_name}) {
print join( ':', $column_name, $result_counter{$column_name} );
}
}
精彩评论