/    Sign up×
Community /Pin to ProfileBookmark

Advice in how to query database

I have a database of a few hundred users, and they are divided into two groups, leader or not leader, by a boolean in the MySQL database. For the members page, I need to show all the leaders in alphabetical order, and then all the non-leaders in alphabetical order. I’m wondering how to do this.
The way that I’ve done it in the past is to query the database twice, one query looking for leaders, and the other for non-leaders, but that seems very inefficient to me. The other way that just occurred to me was to query the database for all all leaders, and then to iterate through the whole lot of them and decide whether or not they should added to the leaders string, or the non-leaders string:

[code=php]
if ($row[‘leaderornot’]==true){
$leader.=’info from leader’;
}
else {
$nonleader.=’info from non-leader’;
}

echo $leader,'<hr>’,$nonleader;
[/code]

So what I’m wondering is, which is more efficient, or is there a third even better way again?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@calliepeckMay 28.2008 — You should be able to ORDER BY leader ASC, lastname ASC

(or leader DESC depending on which group first)
Copy linkTweet thisAlerts:
@ss1289May 28.2008 — You can do a "order by" on the boolean value and then a "order by" on the lastname.

$query = 'SELECT ... ORDER BY bool, lastname';

This will order your bool values first (or group together), then it will order the lastname within the ordered bool values. I'm not sure if I'm clear on this.
Copy linkTweet thisAlerts:
@Declan1991authorMay 29.2008 — Thanks for that.
×

Success!

Help @Declan1991 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...