displaying grouped items when using count()

This is a discussion on displaying grouped items when using count() within the MySQL Database forums, part of the Database Forums category; Not sure how to explain what it is I am after, so forgive me if the subject does not help. ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-03-2007
Dave
 
Posts: n/a
Default displaying grouped items when using count()

Not sure how to explain what it is I am after, so forgive me if the
subject does not help.

Basically

I have a two tables like so:

table1 (
id int(5) auto_increment primary key,
code char(11),
account_number char(11)
)


table2 (
company_id char(11) primary key,
company_name char(100)
)

I have created the following query:
$getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
table1
JOIN table2 ON table1.account_number = table2.company_id
GROUP BY code ORDER BY COUNT(*) ");

This works as expected and i have a webpage that outputs like so:

Code1 - There are 14 companies that buy this product.
Code5 - There are 12 companies that buy this product.
Code9 - There are 6 companies that buy this product.
Code22 - There is 1 company that buys this product, which is: A0001.

However, I would like to display the account numbers, that have been
grouped against the code when there is more than 1, like this:

Code9 - There are 6 companies that buy this product, which are: A0001,
A0002, A0004, C0020, C00030, P2210

I have done searches on google against COUNT(), but havent come across
anything that seems to be what im looking for. (Not even sure if its
possible).

If anyone can point me in the right direction, would be most grateful.

Thanks
Dave.

Reply With Quote
  #2 (permalink)  
Old 09-03-2007
Rik Wasmus
 
Posts: n/a
Default Re: displaying grouped items when using count()

On Mon, 03 Sep 2007 15:27:10 +0200, Dave
<david.greenhall@praybourne.co.uk> wrote:

> Not sure how to explain what it is I am after, so forgive me if the
> subject does not help.
>
> Basically
>
> I have a two tables like so:
>
> table1 (
> id int(5) auto_increment primary key,
> code char(11),
> account_number char(11)
> )
>
>
> table2 (
> company_id char(11) primary key,
> company_name char(100)
> )
>
> I have created the following query:
> $getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
> table1
> JOIN table2 ON table1.account_number = table2.company_id
> GROUP BY code ORDER BY COUNT(*) ");


Possibly (untested)

SELECT
table1.code,
COUNT(DISTINCT table2.company_id) as 'amount',
GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ' as 'companies'
# or if you like GROUP_CONCAT(DISTINCT table2.company_name SEPARATOR ','
as 'companies'
FROM table1
LEFT JOIN table2
ON table1.account_number = table2.company_id
GROUP BY table1.code
ORDER BY `amount`
--
Rik Wasmus
Reply With Quote
  #3 (permalink)  
Old 09-03-2007
Captain Paralytic
 
Posts: n/a
Default Re: displaying grouped items when using count()

On 3 Sep, 15:14, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Mon, 03 Sep 2007 15:27:10 +0200, Dave
>
>
>
>
>
> <david.greenh...@praybourne.co.uk> wrote:
> > Not sure how to explain what it is I am after, so forgive me if the
> > subject does not help.

>
> > Basically

>
> > I have a two tables like so:

>
> > table1 (
> > id int(5) auto_increment primary key,
> > code char(11),
> > account_number char(11)
> > )

>
> > table2 (
> > company_id char(11) primary key,
> > company_name char(100)
> > )

>
> > I have created the following query:
> > $getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
> > table1
> > JOIN table2 ON table1.account_number = table2.company_id
> > GROUP BY code ORDER BY COUNT(*) ");

>
> Possibly (untested)
>
> SELECT
> table1.code,
> COUNT(DISTINCT table2.company_id) as 'amount',
> GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ' as 'companies'
> # or if you like GROUP_CONCAT(DISTINCT table2.company_name SEPARATOR ', '
> as 'companies'
> FROM table1
> LEFT JOIN table2
> ON table1.account_number = table2.company_id
> GROUP BY table1.code
> ORDER BY `amount`
> --
> Rik Wasmus- Hide quoted text -
>
> - Show quoted text -

Still untested but more likely to run:

SELECT
table1.code,
COUNT(DISTINCT table2.company_id) as `amount`,
GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ') as
`companies`
# or if you like GROUP_CONCAT(DISTINCT table2.company_name
SEPARATOR ', ')
as `companies`
FROM table1
LEFT JOIN table2
ON table1.account_number = table2.company_id
GROUP BY table1.code
ORDER BY `amount`

Reply With Quote
  #4 (permalink)  
Old 09-03-2007
Dave
 
Posts: n/a
Default Re: displaying grouped items when using count()

On 3 Sep, 15:44, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 3 Sep, 15:14, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>
>
>
> > On Mon, 03 Sep 2007 15:27:10 +0200, Dave

>
> > <david.greenh...@praybourne.co.uk> wrote:
> > > Not sure how to explain what it is I am after, so forgive me if the
> > > subject does not help.

>
> > > Basically

>
> > > I have a two tables like so:

>
> > > table1 (
> > > id int(5) auto_increment primary key,
> > > code char(11),
> > > account_number char(11)
> > > )

>
> > > table2 (
> > > company_id char(11) primary key,
> > > company_name char(100)
> > > )

>
> > > I have created the following query:
> > > $getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
> > > table1
> > > JOIN table2 ON table1.account_number = table2.company_id
> > > GROUP BY code ORDER BY COUNT(*) ");

>
> > Possibly (untested)

>
> > SELECT
> > table1.code,
> > COUNT(DISTINCT table2.company_id) as 'amount',
> > GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ' as 'companies'
> > # or if you like GROUP_CONCAT(DISTINCT table2.company_name SEPARATOR ', '
> > as 'companies'
> > FROM table1
> > LEFT JOIN table2
> > ON table1.account_number = table2.company_id
> > GROUP BY table1.code
> > ORDER BY `amount`
> > --
> > Rik Wasmus- Hide quoted text -

>
> > - Show quoted text -

>
> Still untested but more likely to run:
>
> SELECT
> table1.code,
> COUNT(DISTINCT table2.company_id) as `amount`,
> GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ') as
> `companies`
> # or if you like GROUP_CONCAT(DISTINCT table2.company_name
> SEPARATOR ', ')
> as `companies`
> FROM table1
> LEFT JOIN table2
> ON table1.account_number = table2.company_id
> GROUP BY table1.code
> ORDER BY `amount`- Hide quoted text -
>
> - Show quoted text -


Thanks a lot folks, that works really well.. But... Is there a way to
seperate them so that i create a link for each company?
Previously i had an if statement, that if the count was equal to one,
it created a link around the account number that was brought back,
using the account number to link to another page (like: company.php?
account=A0001 )

When i added the new code, it wouldnt work with the if statement, but
once removed it displayed all the account numbers as expected
(thanks) ...just need a way to add links to them (if anyone knows a
way) !!

Dave.

The way it is now,

Reply With Quote
  #5 (permalink)  
Old 09-03-2007
Rik Wasmus
 
Posts: n/a
Default Re: displaying grouped items when using count()

On Mon, 03 Sep 2007 17:24:04 +0200, Dave
<david.greenhall@praybourne.co.uk> wrote:

> On 3 Sep, 15:44, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>> On 3 Sep, 15:14, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>>
>>
>>
>> > On Mon, 03 Sep 2007 15:27:10 +0200, Dave

>>
>> > <david.greenh...@praybourne.co.uk> wrote:
>> > > Not sure how to explain what it is I am after, so forgive me if the
>> > > subject does not help.

>>
>> > > Basically

>>
>> > > I have a two tables like so:

>>
>> > > table1 (
>> > > id int(5) auto_increment primary key,
>> > > code char(11),
>> > > account_number char(11)
>> > > )

>>
>> > > table2 (
>> > > company_id char(11) primary key,
>> > > company_name char(100)
>> > > )

>>
>> > > I have created the following query:
>> > > $getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
>> > > table1
>> > > JOIN table2 ON table1.account_number = table2.company_id
>> > > GROUP BY code ORDER BY COUNT(*) ");

>>
>> > Possibly (untested)

>>
>> > SELECT
>> > table1.code,
>> > COUNT(DISTINCT table2.company_id) as 'amount',
>> > GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ' as

>> 'companies'
>> > # or if you like GROUP_CONCAT(DISTINCT table2.company_name

>> SEPARATOR ', '
>> > as 'companies'
>> > FROM table1
>> > LEFT JOIN table2
>> > ON table1.account_number = table2.company_id
>> > GROUP BY table1.code
>> > ORDER BY `amount`
>> > --
>> > Rik Wasmus- Hide quoted text -

>>
>> > - Show quoted text -

>>
>> Still untested but more likely to run:
>>
>> SELECT
>> table1.code,
>> COUNT(DISTINCT table2.company_id) as `amount`,
>> GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ') as
>> `companies`
>> # or if you like GROUP_CONCAT(DISTINCT table2.company_name
>> SEPARATOR ', ')
>> as `companies`
>> FROM table1
>> LEFT JOIN table2
>> ON table1.account_number = table2.company_id
>> GROUP BY table1.code
>> ORDER BY `amount`- Hide quoted text -
>>
>> - Show quoted text -

>
> Thanks a lot folks, that works really well.. But... Is there a way to
> seperate them so that i create a link for each company?
> Previously i had an if statement, that if the count was equal to one,
> it created a link around the account number that was brought back,
> using the account number to link to another page (like: company.php?
> account=A0001 )
>
> When i added the new code, it wouldnt work with the if statement, but
> once removed it displayed all the account numbers as expected
> (thanks) ...just need a way to add links to them (if anyone knows a
> way) !!


Depends on what code/language you use. In PHP for instance, a simple
explode() could break it up into parts again.

Offcourse converting it into HTML could be done in the query itself, I
would strongly advice against it though, something like:
....
GROUP_CONCAT(
DISTINCT
CONCAT(
'<a href="./company.php?account=',
table2.company_id,
'">',
table2.company_name,
'</a>')
SEPARATOR '') as 'companyurls'
....

--
Rik Wasmus
Reply With Quote
  #6 (permalink)  
Old 09-04-2007
Dave
 
Posts: n/a
Default Re: displaying grouped items when using count()

On 3 Sep, 17:03, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Mon, 03 Sep 2007 17:24:04 +0200, Dave
>
>
>
>
>
> <david.greenh...@praybourne.co.uk> wrote:
> > On 3 Sep, 15:44, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> >> On 3 Sep, 15:14, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:

>
> >> > On Mon, 03 Sep 2007 15:27:10 +0200, Dave

>
> >> > <david.greenh...@praybourne.co.uk> wrote:
> >> > > Not sure how to explain what it is I am after, so forgive me if the
> >> > > subject does not help.

>
> >> > > Basically

>
> >> > > I have a two tables like so:

>
> >> > > table1 (
> >> > > id int(5) auto_increment primary key,
> >> > > code char(11),
> >> > > account_number char(11)
> >> > > )

>
> >> > > table2 (
> >> > > company_id char(11) primary key,
> >> > > company_name char(100)
> >> > > )

>
> >> > > I have created the following query:
> >> > > $getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
> >> > > table1
> >> > > JOIN table2 ON table1.account_number = table2.company_id
> >> > > GROUP BY code ORDER BY COUNT(*) ");

>
> >> > Possibly (untested)

>
> >> > SELECT
> >> > table1.code,
> >> > COUNT(DISTINCT table2.company_id) as 'amount',
> >> > GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ' as
> >> 'companies'
> >> > # or if you like GROUP_CONCAT(DISTINCT table2.company_name
> >> SEPARATOR ', '
> >> > as 'companies'
> >> > FROM table1
> >> > LEFT JOIN table2
> >> > ON table1.account_number = table2.company_id
> >> > GROUP BY table1.code
> >> > ORDER BY `amount`
> >> > --
> >> > Rik Wasmus- Hide quoted text -

>
> >> > - Show quoted text -

>
> >> Still untested but more likely to run:

>
> >> SELECT
> >> table1.code,
> >> COUNT(DISTINCT table2.company_id) as `amount`,
> >> GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ') as
> >> `companies`
> >> # or if you like GROUP_CONCAT(DISTINCT table2.company_name
> >> SEPARATOR ', ')
> >> as `companies`
> >> FROM table1
> >> LEFT JOIN table2
> >> ON table1.account_number = table2.company_id
> >> GROUP BY table1.code
> >> ORDER BY `amount`- Hide quoted text -

>
> >> - Show quoted text -

>
> > Thanks a lot folks, that works really well.. But... Is there a way to
> > seperate them so that i create a link for each company?
> > Previously i had an if statement, that if the count was equal to one,
> > it created a link around the account number that was brought back,
> > using the account number to link to another page (like: company.php?
> > account=A0001 )

>
> > When i added the new code, it wouldnt work with the if statement, but
> > once removed it displayed all the account numbers as expected
> > (thanks) ...just need a way to add links to them (if anyone knows a
> > way) !!

>
> Depends on what code/language you use. In PHP for instance, a simple
> explode() could break it up into parts again.
>
> Offcourse converting it into HTML could be done in the query itself, I
> would strongly advice against it though, something like:
> ...
> GROUP_CONCAT(
> DISTINCT
> CONCAT(
> '<a href="./company.php?account=',
> table2.company_id,
> '">',
> table2.company_name,
> '</a>')
> SEPARATOR '') as 'companyurls'
> ...
>
> --
> Rik Wasmus- Hide quoted text -
>
> - Show quoted text -


Doh!

explode() !! damn sorry for wasting your time. I sorted it with
explode() and a foreach statement, and incase anyone else wonders,
example is below.

if ($query['amount'] > 1) {
echo "<td colspan=2>There are <b>";
echo " " .$query['amount']. " </b>Companies who buy this product </
td><tr><td colspan=3>They are: ";

$companyextract = explode(", ",$query['companies']);
foreach ($companyextract as &$comp) {
echo "<a href=company.php?account=";
echo $comp;
echo "><b>" ;
echo $comp;
echo "</b></a>&nbsp; &nbsp; &nbsp;";
}
} else {
echo "<td colspan=2>There is only <b>";
echo " " .$query['amount']. " </b>Company who buy this product </
td><tr><td colspan=3>That is: ";

$companyextract = explode(", ",$query['companies']);
foreach ($companyextract as &$comp) {
echo "<a href=company.php?account=";
echo $comp;
echo "><b>" ;
echo $comp;
echo "</b></a>";
}

Thanks again for everyones help

Dave.

Reply With Quote
  #7 (permalink)  
Old 09-04-2007
strawberry
 
Posts: n/a
Default Re: displaying grouped items when using count()

On 4 Sep, 13:15, Dave <david.greenh...@praybourne.co.uk> wrote:
> On 3 Sep, 17:03, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>
>
>
>
>
> > On Mon, 03 Sep 2007 17:24:04 +0200, Dave

>
> > <david.greenh...@praybourne.co.uk> wrote:
> > > On 3 Sep, 15:44, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> > >> On 3 Sep, 15:14, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:

>
> > >> > On Mon, 03 Sep 2007 15:27:10 +0200, Dave

>
> > >> > <david.greenh...@praybourne.co.uk> wrote:
> > >> > > Not sure how to explain what it is I am after, so forgive me if the
> > >> > > subject does not help.

>
> > >> > > Basically

>
> > >> > > I have a two tables like so:

>
> > >> > > table1 (
> > >> > > id int(5) auto_increment primary key,
> > >> > > code char(11),
> > >> > > account_number char(11)
> > >> > > )

>
> > >> > > table2 (
> > >> > > company_id char(11) primary key,
> > >> > > company_name char(100)
> > >> > > )

>
> > >> > > I have created the following query:
> > >> > > $getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
> > >> > > table1
> > >> > > JOIN table2 ON table1.account_number = table2.company_id
> > >> > > GROUP BY code ORDER BY COUNT(*) ");

>
> > >> > Possibly (untested)

>
> > >> > SELECT
> > >> > table1.code,
> > >> > COUNT(DISTINCT table2.company_id) as 'amount',
> > >> > GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ' as
> > >> 'companies'
> > >> > # or if you like GROUP_CONCAT(DISTINCT table2.company_name
> > >> SEPARATOR ', '
> > >> > as 'companies'
> > >> > FROM table1
> > >> > LEFT JOIN table2
> > >> > ON table1.account_number = table2.company_id
> > >> > GROUP BY table1.code
> > >> > ORDER BY `amount`
> > >> > --
> > >> > Rik Wasmus- Hide quoted text -

>
> > >> > - Show quoted text -

>
> > >> Still untested but more likely to run:

>
> > >> SELECT
> > >> table1.code,
> > >> COUNT(DISTINCT table2.company_id) as `amount`,
> > >> GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ') as
> > >> `companies`
> > >> # or if you like GROUP_CONCAT(DISTINCT table2.company_name
> > >> SEPARATOR ', ')
> > >> as `companies`
> > >> FROM table1
> > >> LEFT JOIN table2
> > >> ON table1.account_number = table2.company_id
> > >> GROUP BY table1.code
> > >> ORDER BY `amount`- Hide quoted text -

>
> > >> - Show quoted text -

>
> > > Thanks a lot folks, that works really well.. But... Is there a way to
> > > seperate them so that i create a link for each company?
> > > Previously i had an if statement, that if the count was equal to one,
> > > it created a link around the account number that was brought back,
> > > using the account number to link to another page (like: company.php?
> > > account=A0001 )

>
> > > When i added the new code, it wouldnt work with the if statement, but
> > > once removed it displayed all the account numbers as expected
> > > (thanks) ...just need a way to add links to them (if anyone knows a
> > > way) !!

>
> > Depends on what code/language you use. In PHP for instance, a simple
> > explode() could break it up into parts again.

>
> > Offcourse converting it into HTML could be done in the query itself, I
> > would strongly advice against it though, something like:
> > ...
> > GROUP_CONCAT(
> > DISTINCT
> > CONCAT(
> > '<a href="./company.php?account=',
> > table2.company_id,
> > '">',
> > table2.company_name,
> > '</a>')
> > SEPARATOR '') as 'companyurls'
> > ...

>
> > --
> > Rik Wasmus- Hide quoted text -

>
> > - Show quoted text -

>
> Doh!
>
> explode() !! damn sorry for wasting your time. I sorted it with
> explode() and a foreach statement, and incase anyone else wonders,
> example is below.
>
> if ($query['amount'] > 1) {
> echo "<td colspan=2>There are <b>";
> echo " " .$query['amount']. " </b>Companies who buy this product </
> td><tr><td colspan=3>They are: ";
>
> $companyextract = explode(", ",$query['companies']);
> foreach ($companyextract as &$comp) {
> echo "<a href=company.php?account=";
> echo $comp;
> echo "><b>" ;
> echo $comp;
> echo "</b></a>&nbsp; &nbsp; &nbsp;";
> }} else {
>
> echo "<td colspan=2>There is only <b>";
> echo " " .$query['amount']. " </b>Company who buy this product </
> td><tr><td colspan=3>That is: ";
>
> $companyextract = explode(", ",$query['companies']);
> foreach ($companyextract as &$comp) {
> echo "<a href=company.php?account=";
> echo $comp;
> echo "><b>" ;
> echo $comp;
> echo "</b></a>";
> }
>
> Thanks again for everyones help
>
> Dave.- Hide quoted text -
>
> - Show quoted text -


So what was the point of concatenating the result set in the first
place?

Reply With Quote
  #8 (permalink)  
Old 09-04-2007
Captain Paralytic
 
Posts: n/a
Default Re: displaying grouped items when using count()

On 4 Sep, 13:15, Dave <david.greenh...@praybourne.co.uk> wrote:
> explode() !! damn sorry for wasting your time. I sorted it with
> explode() and a foreach statement, and incase anyone else wonders,
> example is below.


With a combination of implode(...,explode(...)) you can do it without
the foreach!

Reply With Quote
  #9 (permalink)  
Old 09-14-2007
Dave
 
Posts: n/a
Default Re: displaying grouped items when using count()

On 4 Sep, 13:41, strawberry <zac.ca...@gmail.com> wrote:
> On 4 Sep, 13:15, Dave <david.greenh...@praybourne.co.uk> wrote:
>
>
>
>
>
> > On 3 Sep, 17:03, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:

>
> > > On Mon, 03 Sep 2007 17:24:04 +0200, Dave

>
> > > <david.greenh...@praybourne.co.uk> wrote:
> > > > On 3 Sep, 15:44, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> > > >> On 3 Sep, 15:14, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:

>
> > > >> > On Mon, 03 Sep 2007 15:27:10 +0200, Dave

>
> > > >> > <david.greenh...@praybourne.co.uk> wrote:
> > > >> > > Not sure how to explain what it is I am after, so forgive me if the
> > > >> > > subject does not help.

>
> > > >> > > Basically

>
> > > >> > > I have a two tables like so:

>
> > > >> > > table1 (
> > > >> > > id int(5) auto_increment primary key,
> > > >> > > code char(11),
> > > >> > > account_number char(11)
> > > >> > > )

>
> > > >> > > table2 (
> > > >> > > company_id char(11) primary key,
> > > >> > > company_name char(100)
> > > >> > > )

>
> > > >> > > I have created the following query:
> > > >> > > $getcount = mysql_query("SELECT account_number,code, COUNT(*) FROM
> > > >> > > table1
> > > >> > > JOIN table2 ON table1.account_number = table2.company_id
> > > >> > > GROUP BY code ORDER BY COUNT(*) ");

>
> > > >> > Possibly (untested)

>
> > > >> > SELECT
> > > >> > table1.code,
> > > >> > COUNT(DISTINCT table2.company_id) as 'amount',
> > > >> > GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ' as
> > > >> 'companies'
> > > >> > # or if you like GROUP_CONCAT(DISTINCT table2.company_name
> > > >> SEPARATOR ', '
> > > >> > as 'companies'
> > > >> > FROM table1
> > > >> > LEFT JOIN table2
> > > >> > ON table1.account_number = table2.company_id
> > > >> > GROUP BY table1.code
> > > >> > ORDER BY `amount`
> > > >> > --
> > > >> > Rik Wasmus- Hide quoted text -

>
> > > >> > - Show quoted text -

>
> > > >> Still untested but more likely to run:

>
> > > >> SELECT
> > > >> table1.code,
> > > >> COUNT(DISTINCT table2.company_id) as `amount`,
> > > >> GROUP_CONCAT(DISTINCT table2.company_id SEPARATOR ', ') as
> > > >> `companies`
> > > >> # or if you like GROUP_CONCAT(DISTINCT table2.company_name
> > > >> SEPARATOR ', ')
> > > >> as `companies`
> > > >> FROM table1
> > > >> LEFT JOIN table2
> > > >> ON table1.account_number = table2.company_id
> > > >> GROUP BY table1.code
> > > >> ORDER BY `amount`- Hide quoted text -

>
> > > >> - Show quoted text -

>
> > > > Thanks a lot folks, that works really well.. But... Is there a way to
> > > > seperate them so that i create a link for each company?
> > > > Previously i had an if statement, that if the count was equal to one,
> > > > it created a link around the account number that was brought back,
> > > > using the account number to link to another page (like: company.php?
> > > > account=A0001 )

>
> > > > When i added the new code, it wouldnt work with the if statement, but
> > > > once removed it displayed all the account numbers as expected
> > > > (thanks) ...just need a way to add links to them (if anyone knows a
> > > > way) !!

>
> > > Depends on what code/language you use. In PHP for instance, a simple
> > > explode() could break it up into parts again.

>
> > > Offcourse converting it into HTML could be done in the query itself, I
> > > would strongly advice against it though, something like:
> > > ...
> > > GROUP_CONCAT(
> > > DISTINCT
> > > CONCAT(
> > > '<a href="./company.php?account=',
> > > table2.company_id,
> > > '">',
> > > table2.company_name,
> > > '</a>')
> > > SEPARATOR '') as 'companyurls'
> > > ...

>
> > > --
> > > Rik Wasmus- Hide quoted text -

>
> > > - Show quoted text -

>
> > Doh!

>
> > explode() !! damn sorry for wasting your time. I sorted it with
> > explode() and a foreach statement, and incase anyone else wonders,
> > example is below.

>
> > if ($query['amount'] > 1) {
> > echo "<td colspan=2>There are <b>";
> > echo " " .$query['amount']. " </b>Companies who buy this product </
> > td><tr><td colspan=3>They are: ";

>
> > $companyextract = explode(", ",$query['companies']);
> > foreach ($companyextract as &$comp) {
> > echo "<a href=company.php?account=";
> > echo $comp;
> > echo "><b>" ;
> > echo $comp;
> > echo "</b></a>&nbsp; &nbsp; &nbsp;";
> > }} else {

>
> > echo "<td colspan=2>There is only <b>";
> > echo " " .$query['amount']. " </b>Company who buy this product </
> > td><tr><td colspan=3>That is: ";

>
> > $companyextract = explode(", ",$query['companies']);
> > foreach ($companyextract as &$comp) {
> > echo "<a href=company.php?account=";
> > echo $comp;
> > echo "><b>" ;
> > echo $comp;
> > echo "</b></a>";
> > }

>
> > Thanks again for everyones help

>
> > Dave.- Hide quoted text -

>
> > - Show quoted text -

>
> So what was the point of concatenating the result set in the first
> place?- Hide quoted text -
>
> - Show quoted text -


Strawberry,
(sorry its been so long to get back, but been away)
I use what i know to do the job, when i dont know something i find out
how to do it. What i found did the job perfectly. I grouped the
results to count them in one section of the page. Then in another
section, display the account codes.


Reply With Quote
  #10 (permalink)  
Old 09-14-2007
Dave
 
Posts: n/a
Default Re: displaying grouped items when using count()

On 4 Sep, 13:42, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 4 Sep, 13:15, Dave <david.greenh...@praybourne.co.uk> wrote:
>
> > explode() !! damn sorry for wasting your time. I sorted it with
> > explode() and a foreach statement, and incase anyone else wonders,
> > example is below.

>
> With a combination of implode(...,explode(...)) you can do it without
> the foreach!


Captain Paralytic,

I appreciate the help, I am learning as i go along, and knew about
explode and foreach. I didnt know that i could use implode at the same
time as explode.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:50 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0