/    Sign up×
Community /Pin to ProfileBookmark

MySQL Query Grouping in PHP

Hey,
This question is more suited I think to MySQL, but as I didn’t see a forum for that (I may just be blind and tired) I decided to post here, as I am calling this MySQL query from PHP.

Anyway, my question is this:
I am trying to get a drop-down box to populate from a MySQL db. The table is set up with an id primary key, but this is only because I can’t use multiple primary keys. I have two columns that I’m searching by: col_a and col_b, but I want to order by col_a.
Now, as primary keys go, there are instances when there are multiple rows that have the same value in col_a, but not col_a AND col_b.

I want to run a MySQL query that’s similar to this:

[code=php]
$query = “SELECT col_a, col_b FROM table ORDER BY col_a ASC”;
[/code]

That works great, except, then I want to order by col_b (after it order’s by col_a)
Also, I want to somehow group by col_a and limit the number of rows that appear in a col_a group, but not the whole table. The query I envision looks something like this:

[code=php]
$query = “SELECT col_a, col_b FROM table GROUP BY col_a ORDER BY col_a ASC LIMIT 2”;
[/code]

so that a drop down box would have something like this in it:
row 1: box 2000
row 2: box 1999
row 3: scissors 1999
row 4: tape 2004

but instead, with the above query, I get:
row 1: box 2000
row 2: scissors 1999

Any idea how I can accomplish this SQL statement? I probably haven’t been very clear and I can elaborate if the need arises.

Thanks for any help,
Jaelan

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsDec 01.2004 — do mean something like this:SELECT col_a, col_b FROM table ORDER BY col_a ASC, col_b ASC
Copy linkTweet thisAlerts:
@JaelanauthorDec 01.2004 — Yep, thanks alot. That did what I wanted.

I guess I was making it more complex than it was.

Jaelan
Copy linkTweet thisAlerts:
@96turnerriDec 01.2004 — hmm an SQL forum mite not be a bad idea
Copy linkTweet thisAlerts:
@JaelanauthorDec 01.2004 — I obviously just recently signed onto the forum, but I was amazed by all the various forums, and then depressed there wasn't one for MySQL (cause I'm a web programmer that works mostly w/ php and mysql). But I wasn't too broken up about it.

Cast my vote for an SQL forum.

Jaelan
×

Success!

Help @Jaelan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...