Unix Technical Forum

View select results

This is a discussion on View select results within the MySQL General forum forums, part of the MySQL category; --> Hi my table have three fields that, if selected, are shown like: area1, value_one, thing_one area1, value_two, thing_32 area1, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:18 AM
spacemarc
 
Posts: n/a
Default View select results

Hi
my table have three fields that, if selected, are shown like:

area1, value_one, thing_one
area1, value_two, thing_32
area1, value_three, thing_ dd
area2, value_ten, thing_6w
area2, value_ff, thing_l
.....
.....

can I obtain a recordset like this?

area1, value_one, thing_one
//, value_two, thing_32
//, value_three, thing_ dd
area2, value_ten, thing_6w
//, value_ff, thing_l

So, do not repeat more times the value of the first column (area1, area2...)

--
http://www.spacemarc.it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:18 AM
Baron Schwartz
 
Posts: n/a
Default Re: View select results

Hi,

spacemarc wrote:
> Hi
> my table have three fields that, if selected, are shown like:
>
> area1, value_one, thing_one
> area1, value_two, thing_32
> area1, value_three, thing_ dd
> area2, value_ten, thing_6w
> area2, value_ff, thing_l
> ....
> ....
>
> can I obtain a recordset like this?
>
> area1, value_one, thing_one
> //, value_two, thing_32
> //, value_three, thing_ dd
> area2, value_ten, thing_6w
> //, value_ff, thing_l
>
> So, do not repeat more times the value of the first column (area1,
> area2...)


Giuseppe Maxia wrote a great article on this some time ago. The technique is called
cross-tabulation or pivot tables. Here is a link:
http://www.onlamp.com/pub/a/onlamp/2...crosstabs.html

Baron
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:18 AM
Peter Brawley
 
Posts: n/a
Default Re: View select results

>can I obtain a recordset like this?
>area1, value_one, thing_one
> //, value_two, thing_32
> //, value_three, thing_ dd
>area2, value_ten, thing_6w
> //, value_ff, thing_l


SET @prev='';
SELECT
IF(area = @prev, '', @prev := area) AS area,
... other columns ...
FROM &c ...

PB


spacemarc wrote:
> Hi
> my table have three fields that, if selected, are shown like:
>
> area1, value_one, thing_one
> area1, value_two, thing_32
> area1, value_three, thing_ dd
> area2, value_ten, thing_6w
> area2, value_ff, thing_l
> ....
> ....
>
> can I obtain a recordset like this?
>
> area1, value_one, thing_one
> //, value_two, thing_32
> //, value_three, thing_ dd
> area2, value_ten, thing_6w
> //, value_ff, thing_l
>
> So, do not repeat more times the value of the first column (area1,
> area2...)
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:18 AM
spacemarc
 
Posts: n/a
Default Re: View select results

2007/5/2, Peter Brawley <peter.brawley@earthlink.net>:
> Works for me. Please post a CREATE TABLE stmt & enough INSERTs to
> demonstrate the problem.


This is the dump (MySQL: 5.0.38): the table is not final version, just
to test the query.

CREATE TABLE `products` (
`area` varchar(25) NOT NULL,
`text` varchar(25) NOT NULL,
`amount` int(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `products` (`area`, `text`, `amount`) VALUES
('area1', 'some text', 12),
('area1', 'other text here', 13),
('area3', 'example...', 22),
('area2', 'things', 123),
('area1', 'bla bla...', 24),
('area2', 'others again', 231),
('area1', 'english language..', 44),
('area1', 'server database', 53),
('area3', 'php language...', 22),
('area2', 'linux box', 951),
('area1', 'developer tools', 4),
('area2', 'others words', 1);



--
http://www.spacemarc.it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 06:18 AM
Peter Brawley
 
Posts: n/a
Default Re: View select results

Right, give the computed column an alias differeing from the column name, eg

SET @prev='';
SELECT
IF(area = @prev, '', @prev := area) AS AreaHdr,
text,amount
FROM products
ORDER BY area DESC;

PB

spacemarc wrote:
> 2007/5/2, Peter Brawley <peter.brawley@earthlink.net>:
>> Works for me. Please post a CREATE TABLE stmt & enough INSERTs to
>> demonstrate the problem.

>
> This is the dump (MySQL: 5.0.38): the table is not final version, just
> to test the query.
>
> CREATE TABLE `products` (
> `area` varchar(25) NOT NULL,
> `text` varchar(25) NOT NULL,
> `amount` int(3) NOT NULL
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
>
> INSERT INTO `products` (`area`, `text`, `amount`) VALUES
> ('area1', 'some text', 12),
> ('area1', 'other text here', 13),
> ('area3', 'example...', 22),
> ('area2', 'things', 123),
> ('area1', 'bla bla...', 24),
> ('area2', 'others again', 231),
> ('area1', 'english language..', 44),
> ('area1', 'server database', 53),
> ('area3', 'php language...', 22),
> ('area2', 'linux box', 951),
> ('area1', 'developer tools', 4),
> ('area2', 'others words', 1);
>
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 06:18 AM
spacemarc
 
Posts: n/a
Default Re: View select results

2007/5/2, Peter Brawley <peter.brawley@earthlink.net>:
> Right, give the computed column an alias differeing from the column name, eg
>
> SET @prev='';
> SELECT
> IF(area = @prev, '', @prev := area) AS AreaHdr,
> text,amount
> FROM products
> ORDER BY area DESC;


ok, now it works! thanks!

One last thing: you set, at first, a parameter called @prev with Null
(' ') value: right?

And, after, you use, instead IF ELSE statement, another syntax: is it
trinary operator? if yes, why it not is in the online MySQL manual?

--
http://www.spacemarc.it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 06:18 AM
Peter Brawley
 
Posts: n/a
Default Re: View select results

>One last thing: you set, at first, a parameter called @prev with Null
>(' ') value: right?


No, I set it to a string containing one space char. Use anything that
does not occur as data in the column.

>And, after, you use, instead IF ELSE statement, another syntax: is it
>trinary operator? if yes, why it not is in the online MySQL manual?


I used the IF() function, see 'Control Flow Functions' under 'Functions
and Operators' in the manual

PB

------

spacemarc wrote:
> 2007/5/2, Peter Brawley <peter.brawley@earthlink.net>:
>> Right, give the computed column an alias differeing from the column
>> name, eg
>>
>> SET @prev='';
>> SELECT
>> IF(area = @prev, '', @prev := area) AS AreaHdr,
>> text,amount
>> FROM products
>> ORDER BY area DESC;

>
> ok, now it works! thanks!
>
> One last thing: you set, at first, a parameter called @prev with Null
> (' ') value: right?
>
> And, after, you use, instead IF ELSE statement, another syntax: is it
> trinary operator? if yes, why it not is in the online MySQL manual?
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:30 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com