DB에 저장된 이름중에 한개씩만 출력하려면... 정보
그누보드 DB에 저장된 이름중에 한개씩만 출력하려면...본문
-------------------------------------
uid | name | point | kind |
1 | 둘리 | 3 | ca |
2 | 또치 | 5 | ca |
3 | 둘리 | 5 | ca |
4 | 마이콜 | 4 | ca |
5 | 또치 | 2 | ca |
-------------------------------------
가령 위와같이 AAA라는 테이블이 있다구 할적에 각 레코드별 name이 같은 것들끼리 평균값을 내려구 하거든요.
그래서 결과물이 나오긴 했는데
---------------------------------------
이름 | 평균점수 (별점) | 평가인수
---------------------------------------
둘리 | 4 (★★★★) | 2
---------------------------------------
또치 | 3.5 (★★★) | 2
---------------------------------------
마이콜 | 4 (★★★★) | 1
---------------------------------------
둘리 | 4 (★★★★) | 2
---------------------------------------
또치 | 3.5 (★★★) | 2
---------------------------------------
위처럼 나오네요. --;
결론적으로 말씀드리자면 평가된 name값들중에 각 1개씩만 평균을 내어 출력하고자 합니다.
한수 가르침을 주시면 감사하겠습니다.
위에 사용된 소스입니다.
<?
$rque=mysql_query("select * from AAA where kind='ca'") or die(mysql_error());
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr height=22>
<TD>이름</TD><TD>평균점수(별점)</TD><TD align=center>평가人수</TD>
</tr>
<?
while($rs=mysql_fetch_array($rque)) {
$star="";
$avg=@mysql_fetch_array(mysql_query("select avg(point) from AAA where name='$rs[name]' and point > 0"));
for($i=1;$i<=doubleval($avg[0]);$i++) $star.="★";
$count=@mysql_fetch_array(mysql_query("select count(point) from AAA where name='$rs[name]' and point > 0"));
?>
<tr>
<td class=sub_a><?=$rs[name]?></td>
<td><?=doubleval($avg[0])?>(<?=$star?>)</TD>
<td align=center class=sub_a><?=$count[0]?> 명</td>
</tr>
<?}?>
</table>
uid | name | point | kind |
1 | 둘리 | 3 | ca |
2 | 또치 | 5 | ca |
3 | 둘리 | 5 | ca |
4 | 마이콜 | 4 | ca |
5 | 또치 | 2 | ca |
-------------------------------------
가령 위와같이 AAA라는 테이블이 있다구 할적에 각 레코드별 name이 같은 것들끼리 평균값을 내려구 하거든요.
그래서 결과물이 나오긴 했는데
---------------------------------------
이름 | 평균점수 (별점) | 평가인수
---------------------------------------
둘리 | 4 (★★★★) | 2
---------------------------------------
또치 | 3.5 (★★★) | 2
---------------------------------------
마이콜 | 4 (★★★★) | 1
---------------------------------------
둘리 | 4 (★★★★) | 2
---------------------------------------
또치 | 3.5 (★★★) | 2
---------------------------------------
위처럼 나오네요. --;
결론적으로 말씀드리자면 평가된 name값들중에 각 1개씩만 평균을 내어 출력하고자 합니다.
한수 가르침을 주시면 감사하겠습니다.
위에 사용된 소스입니다.
<?
$rque=mysql_query("select * from AAA where kind='ca'") or die(mysql_error());
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr height=22>
<TD>이름</TD><TD>평균점수(별점)</TD><TD align=center>평가人수</TD>
</tr>
<?
while($rs=mysql_fetch_array($rque)) {
$star="";
$avg=@mysql_fetch_array(mysql_query("select avg(point) from AAA where name='$rs[name]' and point > 0"));
for($i=1;$i<=doubleval($avg[0]);$i++) $star.="★";
$count=@mysql_fetch_array(mysql_query("select count(point) from AAA where name='$rs[name]' and point > 0"));
?>
<tr>
<td class=sub_a><?=$rs[name]?></td>
<td><?=doubleval($avg[0])?>(<?=$star?>)</TD>
<td align=center class=sub_a><?=$count[0]?> 명</td>
</tr>
<?}?>
</table>
댓글 전체
DISTINCT name 을 사용하시면 됩니다.
<?
$rque=mysql_query("select DISTINCT name from AAA where kind='ca'") or die(mysql_error());
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr height=22>
<TD>이름</TD><TD>평균점수(별점)</TD><TD align=center>평가人수</TD>
</tr>
<?
while($rs=mysql_fetch_array($rque)) {
$star="";
$avg=@mysql_fetch_array(mysql_query("select avg(point) from AAA where name='$rs[name]' and point > 0"));
for($i=1;$i<=doubleval($avg[0]);$i++) $star.="★";
$count=@mysql_fetch_array(mysql_query("select count(point) from AAA where name='$rs[name]' and point > 0"));
?>
<tr>
<td class=sub_a><?=$rs[name]?></td>
<td><?=doubleval($avg[0])?>(<?=$star?>)</TD>
<td align=center class=sub_a><?=$count[0]?> 명</td>
</tr>
<?}?>
</table>
<?
$rque=mysql_query("select DISTINCT name from AAA where kind='ca'") or die(mysql_error());
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr height=22>
<TD>이름</TD><TD>평균점수(별점)</TD><TD align=center>평가人수</TD>
</tr>
<?
while($rs=mysql_fetch_array($rque)) {
$star="";
$avg=@mysql_fetch_array(mysql_query("select avg(point) from AAA where name='$rs[name]' and point > 0"));
for($i=1;$i<=doubleval($avg[0]);$i++) $star.="★";
$count=@mysql_fetch_array(mysql_query("select count(point) from AAA where name='$rs[name]' and point > 0"));
?>
<tr>
<td class=sub_a><?=$rs[name]?></td>
<td><?=doubleval($avg[0])?>(<?=$star?>)</TD>
<td align=center class=sub_a><?=$count[0]?> 명</td>
</tr>
<?}?>
</table>