채택완료

mysql 데이터출력후 그룹별로 묶어서 정렬하는방법좀 알려주세요.

2014-05-03 (토) 17:37:41 9,291
mysql에서 원하는 정보의 데이터를 불러옵니다.
 
<table>
$sql = "select * 테이블명 where id > 10 order by code asc";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++)
{
    <tr>
    <td>code</td><td>id</td><td>name</td><td>year</td>
    </tr>
}
</table>
 
code     id    name    year
001        1     kim         1
002        2     lee         2
003        4     kim         1
004        6     kim         1
005        5     lee         2
006        7     choi       2
--------------------------------
위와 같이 데이터가 출력됩니다만,
 
이렇게 출력되는 데이터를 year로 묶어서 정렬하는 방법은 어떻게 되나요..?
 
제가 원하는 결과값이 아래처럼 한페이지에 나오게 하는 방법좀 알려주세요
 
code     id    name    year
001        1     kim         1
003        4     kim         1
004        6     kim         1
-------------------------
code     id    name    year
002        2     lee         2
005        5     lee         2
006        7     choi       2
 
 
<table>시작해서</table> 로 끝나는게 year 기준으로 1년 짜리가 정렬되어야 하구요...

다시

<table> 시작해서 </table> 로 끝나는게 year 2년짜리가 정렬되어야 되거든요..

즉, 1년짜리가 한묶음이 되는거고 다음 2년짜리가 한묶음이 되어야 되는데....
각각 다른 테이블에 따로 표시되어야 하는데 방법이 없을까요?   한페이지에 나와야 하는데 방법좀 알려주세요
 
|

답변 4개 / 댓글 4개

채택된 답변
+20 포인트
코드 나갑니다.
 
           
<table>
$sql = "select * 테이블명 where id > 10 order by code, year asc";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++)
{
  if($row[$i][3] == 1){
?>
    <tr>
    <td>code</td><td>id</td><td>name</td><td>year</td>
    <td><?echo $row[$i][0];?></td><td><?echo $row[$i][1];?></td>><td><?echo $row[$i][2];?></td>><td><?echo $row[$i][3];?></td>
    </tr>
<?}?>
 
 
for ($i=0; $row=sql_fetch_array($result); $i++)
{
  if($row[$i][3] == 2){
?>
    <tr>
    <td>code</td><td>id</td><td>name</td><td>year</td>
    <td><?echo $row[$i][0];?></td><td><?echo $row[$i][1];?></td>><td><?echo $row[$i][2];?></td>><td><?echo $row[$i][3];?></td>
    </tr>
<?}?>
 
</table>
 
-- 뺑이를 2번치세요

답변에 대한 댓글 1개

답변감사합니다....

알려주신데로 하니 안되네요....ㅠㅠ
year 필드의 설정이 VARCHAR 형태라면 ..
 
<table>
$sql = "select * 테이블명 where id > 10 order by code, year asc";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++)
{
  if($row[$i][3] == "1"){
?>
    <tr>
    <td>code</td><td>id</td><td>name</td><td>year</td>
    <td><?echo $row[$i][0];?></td><td><?echo $row[$i][1];?></td>><td><?echo $row[$i][2];?></td>><td><?echo $row[$i][3];?></td>
    </tr>
<?}?>
 
 
for ($i=0; $row=sql_fetch_array($result); $i++)
{
  if($row[$i][3] == "2"){
?>
    <tr>
    <td>code</td><td>id</td><td>name</td><td>year</td>
    <td><?echo $row[$i][0];?></td><td><?echo $row[$i][1];?></td>><td><?echo $row[$i][2];?></td>><td><?echo $row[$i][3];?></td>
    </tr>
<?}?>
 
</table>

답변에 대한 댓글 1개

답변감사합니다.

$row[$i][3] 이라는게 안되네요....

알려주신데로 하니, 디비에 저장된 값이 나오는게 아니라
<td>[0]</td><td>[1]></td><td>[2]</td> <== 이렇게 나오네요....ㅜㅜ
order by year asc id asc

답변에 대한 댓글 1개

답변 감사합니다.

위에 말씀드린데로 단순한 정렬이 아니구여...

year를 기준으로 루프?? 를 돌아야 합니다.....
혹시 아시면 방법좀 알려주세요.....
<table>
$sql = "select * 테이블명 where id > 10 order by code, year asc";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++)
{
    <tr>
    <td>code</td><td>id</td><td>name</td><td>year</td>
    </tr>
}
</table>
 
 
혹시 order by 구문에 대하여 알고 계신지요... filed 가 code, year 로 정렬을 2번 하면되겟죠..
$sql = "select * 테이블명 where id > 10 order by code, year asc";

답변에 대한 댓글 1개

답변감사합니다.

네 말씀하신데로 order by code, year asc 이렇게 하면...

한 페이지에서 code와 year을 정렬하는 방법은 알고 있습니다.
음....제가 설명이 부족했나봅니다..

<table> year 1년짜리 </table> 이렇게 1년짜리가 루프돌고
<table> year 2년짜리 </table> 이렇게 2년짜리가 루프돌고

한페이지에 저렇게 year을 기준으로 묶어서 루프돌게끔 하려고 합니다.
혹시 아시면 방법좀 알려주세요....

답변을 작성하려면 로그인이 필요합니다.