dbTalk Databases Forums  

Grouping and returning rows

comp.databases.mysql comp.databases.mysql


Discuss Grouping and returning rows in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
UKuser
 
Posts: n/a

Default Grouping and returning rows - 01-09-2007 , 06:37 AM






Hi folks,

I have a db which is similar to the example

Name | Country | Type
test | USA | Nm
test2 | UK | Nb
test3 | USA | Nc
test4 | USA | Nm

What I want to know is if there is a simple SQL statement to do the
following:

1) Select all entries where country = USA (for example)
2) Group by Type
3) Display each group under each heading Type - i.e.

Nm
test | USA
test4 | USA

Nc
test3 | USA

I'm going to keep looking, but I want to keep my SQL to a minimum and
wondered what the official way of doing this would be.

Thanks

A


Reply With Quote
  #2  
Old   
strawberry
 
Posts: n/a

Default Re: Grouping and returning rows - 01-09-2007 , 06:53 AM







UKuser wrote:

Quote:
Hi folks,

I have a db which is similar to the example

Name | Country | Type
test | USA | Nm
test2 | UK | Nb
test3 | USA | Nc
test4 | USA | Nm

What I want to know is if there is a simple SQL statement to do the
following:

1) Select all entries where country = USA (for example)
2) Group by Type
3) Display each group under each heading Type - i.e.

Nm
test | USA
test4 | USA

Nc
test3 | USA

I'm going to keep looking, but I want to keep my SQL to a minimum and
wondered what the official way of doing this would be.

Thanks

A
Item 3 is the tricky one, although it would be easy with a bit of php
(or similar, presumably) provided that's available to you.

Anyway, here's the query:

SELECT type,name,country FROM table WHERE country = 'USA' GROUP BY type
ORDER BY type



Reply With Quote
  #3  
Old   
UKuser
 
Posts: n/a

Default Re: Grouping and returning rows - 01-09-2007 , 06:55 AM



Hi,

Thanks for the SQL. It was point 3 which I wanted to see if it could be
done in the same SQL statement or if it was separate.

A


strawberry wrote:
Quote:
UKuser wrote:

Hi folks,

I have a db which is similar to the example

Name | Country | Type
test | USA | Nm
test2 | UK | Nb
test3 | USA | Nc
test4 | USA | Nm

What I want to know is if there is a simple SQL statement to do the
following:

1) Select all entries where country = USA (for example)
2) Group by Type
3) Display each group under each heading Type - i.e.

Nm
test | USA
test4 | USA

Nc
test3 | USA

I'm going to keep looking, but I want to keep my SQL to a minimum and
wondered what the official way of doing this would be.

Thanks

A

Item 3 is the tricky one, although it would be easy with a bit of php
(or similar, presumably) provided that's available to you.

Anyway, here's the query:

SELECT type,name,country FROM table WHERE country = 'USA' GROUP BY type
ORDER BY type


Reply With Quote
  #4  
Old   
tgmayfield
 
Posts: n/a

Default Re: Grouping and returning rows - 01-09-2007 , 08:57 PM



UKuser wrote:
Quote:
Hi,

Thanks for the SQL. It was point 3 which I wanted to see if it could be
done in the same SQL statement or if it was separate.

It _can_ be done, with generated SQL in stored procedures and temporary
tables, but it's usually not worth the overhead of writing,
maintaining, and securing it. I've never tried to execute generated SQL
in a MySQL stored procedure, so I'm not even sure it has a function to
do so.

--Thomas



Reply With Quote
  #5  
Old   
Sanders Kaufman
 
Posts: n/a

Default Re: Grouping and returning rows - 01-10-2007 , 01:12 AM



UKuser wrote:

Quote:
What I want to know is if there is a simple SQL statement to do the
following:

1) Select all entries where country = USA (for example)
2) Group by Type
3) Display each group under each heading Type - i.e.

I always get confused when folks talk about SQL results as what
is "displayed".
I think more in set-based logic, or how it's structured.

What do you mean when you say "Display each group under each
heading"?


Reply With Quote
  #6  
Old   
Sanders Kaufman
 
Posts: n/a

Default Re: Grouping and returning rows - 01-10-2007 , 01:14 AM



strawberry wrote:

Quote:
Anyway, here's the query:

SELECT type,name,country FROM table WHERE country = 'USA' GROUP BY type
ORDER BY type
I get all muddle-headed with this whole GROUP BY thing.
What does the record of a GROUP BY query look like? Does it
have a special column, or go beyond just two-dimensional tables?


Reply With Quote
  #7  
Old   
UKuser
 
Posts: n/a

Default Re: Grouping and returning rows - 01-10-2007 , 03:36 AM



Hi Folks,

Thanks for your advice. I've solved the problem by doing the following:

1) SQL(1) to select distinct headers from database
2) Print SQL(1)->headers
3) Execute a second (sub) SQL statement SQL(2) *foreach* SQL(1)
4) Print SQL(2)->name (i.e. unique records)
5) SQL(2)->MoveNext();
6) SQL(1)->MoveNext();

I'm using ADODB and can HIGHLY recommend it as I'm working inhouse with
Access and live will be MySQL and can change with a very simple coding
switch. Its probably not pushing the db to its limits (i.e.
transactions and stuff which could be restricted by ADODB) but for
small-medium dbs (and probably some larger ones) is great.

Anyway - hope my thoughts help. I've not posted the code as its ADODB
and probably irrelevant for most of you.

Cheers

A

Sanders Kaufman wrote:

Quote:
strawberry wrote:

Anyway, here's the query:

SELECT type,name,country FROM table WHERE country = 'USA' GROUP BY type
ORDER BY type

I get all muddle-headed with this whole GROUP BY thing.
What does the record of a GROUP BY query look like? Does it
have a special column, or go beyond just two-dimensional tables?


Reply With Quote
  #8  
Old   
Bill Karwin
 
Posts: n/a

Default Re: Grouping and returning rows - 01-10-2007 , 10:55 AM



tgmayfield wrote:
Quote:
I've never tried to execute generated SQL
in a MySQL stored procedure, so I'm not even sure it has a function to
do so.
FYI:
http://dev.mysql.com/doc/refman/5.0/en/sqlps.html


Regards,
Bill K.


Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.