LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2005, 12:17 AM   #1
robgo777
Member
 
Registered: Nov 2002
Posts: 35

Rep: Reputation: 15
Quick SQL question


I have a SELECT SQL that returns the following result:

Gender Number

Female: 15967
Male: 19739

The SQL is as follows:

SELECT gender, COUNT(gender) AS number FROM links GROUP BY gender ORDER BY gender


Now what I want hopefully should be pretty easy. I want to be able to associate the Gender results with a particular color. So for example, the result I need is:

Gender Number Color

Female: 15967 #62ACD7
Male: 19739 #38DA9F

This should be able to be accomplished without creating another field in the links table called Color. This should all fall within the SELECT query I think??? Also, I need for this to also work with queries that return more than 2 rows, i.e.

Country: Number Color

Albania: 24 #62ACD7
Algeria: 37 #38DA9F
Argentina: 15 #38DA3C
Australia: 272 #C9DA38


Thank you so much. If there is some php programming required to get this result, then that would work fine as well, but I am hoping this can all be accomplished with the SELECT query.

Robert
 
Old 01-08-2005, 04:18 AM   #2
Inunu
LQ Newbie
 
Registered: Jan 2005
Location: NZ
Distribution: Customized MDK 9
Posts: 13

Rep: Reputation: 0
Hi Robgo.

Using another table that explicitly defines the association between your first column and colour value and then applying SELECT is probably the most common. It may not be what you after but practical.

CREATE TABLE colour (
type VARCHAR(20) NOT NULL PRIMARY KEY,
value CHAR(7) NOT NULL
);
INSERT INTO colour VALUES('Female', '#62ACD7');
INSERT INTO colour VALUES('Male', '#38DA9F');

SELECT l.gender, COUNT(l.gender) AS number, c.value FROM links as l, colour as c GROUP BY gender ORDER BY gender WHERE l.gender=c.type;


If colour value is not dependent on the first column (i.e. you just want ppl to read more clearly), you can simply colour it up in your PHP or other programming code when handling output of your SELECT query.

$colours=array("#62ACD7", "#38DA9F", "#38DA3C", "#C9DA38"...);
$colours_len=count($colours);

mysql_connect(...);
$result=mysql_query("SELECT ...");
if($result){
$n=$mysql_num_rows($result);
for($i=0; $i<$n; $i++){
list($country, $number, $colour)=mysql_fetch_row($result);
print($count . ": " .$number . " " . $colours[$i % $colours_len]);
}
}

IMHO the nature of your intended task requires some level of iterating, not really SELECT's job. Hope that does you some help.
 
Old 01-08-2005, 10:39 AM   #3
robgo777
Member
 
Registered: Nov 2002
Posts: 35

Original Poster
Rep: Reputation: 15
Hi,

I added the new table as suggested, but I keep getting errors using your SELECT query...

SQL-query:

SELECT l.gender, COUNT( l.gender ) AS number, c.value
FROM links AS l, colour AS c
GROUP BY gender
ORDER BY gender
WHERE l.gender = c.type
LIMIT 0 , 30
MySQL said:

#1064 - You have an error in your SQL syntax near 'WHERE l.gender=c.type LIMIT 0, 30' at line 1

I don't want to color the actual results, I just need the columb color to return in the results next to gender and number.

Thanks,

Robert
 
Old 01-08-2005, 11:11 AM   #4
Inunu
LQ Newbie
 
Registered: Jan 2005
Location: NZ
Distribution: Customized MDK 9
Posts: 13

Rep: Reputation: 0
Oups, try this instead please.

Code:
SELECT q.gender, q.count, c.value FROM 
    (SELECT gender, COUNT(gender) as count FROM link GROUP BY gender ORDER BY gender) as q,
    colour as c, WHERE q.gender=c.type;
This way we do the match between the result of gender count (2 rows) and colour (2 rows as well).
 
  


Reply



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 On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sql plus question puishor Linux - Software 3 10-23-2005 02:09 PM
SQL question jpc82 Programming 5 08-13-2005 11:05 AM
ODBC sql.h question javakid Programming 0 11-16-2004 08:34 AM
quick easy SQL qn cadj Programming 1 08-19-2004 07:15 AM
SQL Question jacksmash Programming 7 01-18-2004 10:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration