ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.