0

I'm somewhat new with php/mysql so I'm sure I'm missing something. This approach probably isn't the best either.

I have the following tables

Table: unit_members Fields: id, unit_id, person_id, primary

Table: persons Fields: id, name, rank_id

Table: ranks Fields: id, name, rank

Table: units Fields: id, name, rank

I'm trying to display each unit and the persons in each unit and have the persons sorted by rank which is an int field. It seems simple, but I'm unable to get the persons to be ordered by rank.rank.

Here's my query:

'SELECT unit_members.person_id, GROUP_CONCAT(persons.id) as personid, 
        GROUP_CONCAT(persons.name) as name, persons.rank_id, unit_members.unit_id, 
        units.id as unitid, units.name as unit, 
        GROUP_CONCAT(DISTINCT units.codename) as squad, ranks.id as rankid, 
        GROUP_CONCAT(ranks.rank) as rankrank 
FROM unit_members, ranks, persons, units
where persons.rank_id = ranks.id 
  and units.id = unit_members.unit_id 
  and persons.id = unit_members.person_id
group by units.codename
ORDER BY units.rank asc, ranks.rank asc';

It appears to me that the Order by here does not affect the Group_Concat, so my guess is I need to Order the Group_Concat somehow before it goes into an array. The array seems to order it by person id.

Any help would be greatly appreciated. Thanks in advance.

4

1 回答 1

0

我想你只是有一些误解。

ORDER BY不会影响GROUP_CONCAT(). 您可以通过指定括号内的顺序来更改GROUP_CONCAT()函数中结果的顺序。

就目前而言,您的查询将按 unit.codename 分组,并且在每个 unit.codename 内,任意结果将首先按units.rank 排序,然后按ranks.rank 排序。

一个更大的问题:你一直在谈论一个数组。这里没有数组。

于 2011-05-01T02:39:05.227 回答