[请益] PHP+MYSQL问题

楼主: ftseng (F大叔)   2014-05-04 11:46:59
请益各位高手大大,
有3个相同的column的表格,但是内容不同。表格如下:
a2 a4 a6
pid session a2a a2b pid session a4a a4b pid session a6a a6b
==================== ==================== ====================
1 test 1 0 1 test 0 0 1 test 1 0
2 try 0 1 2 try 1 1 2 try 0 1
合并3个表格成为一个的程式如下:
SELECT a2.session,a2.a2a,a2.a2b,a4.a4a,a4.a4b,a6.a6a,a6.a6b
FROM a2
JOIN a4 ON a4.session = a2.session
JOIN a6 ON a6.session = a4.session
得到的结果就是:
session a2a a2b a4a a4b a6a a6b
================================
test 1 0 0 0 1 0
try 0 1 1 1 0 1
我想请益的问题,我想要做每个session的平均值该如何写?
session a2a a2b a4a a4b a6a a6b avg
====================================
test 1 0 0 0 1 0 ?
try 0 1 1 1 0 1 ?
我写的语法是:
SELECT "a2.session,a2.a2a,a2.a2b,a4.a4a,a4.a4b,a6.a6a,a6.a6b
AND AVG(a2.a2a+a2.a2b+a4.a4a+a4.a4b+a6.a6a+a6.a6b)/6
FROM a2
JOIN a4 ON a4.session = a2.session
JOIN a6 ON a6.session = a4.session
WHERE `session` LIKE '".$_SESSION["loginMember"]."'";
<table width="1200" border="1">
<tr>
<td align='center'><div align="center">session</div></td>
<td align='center'><div align="center">a2a</div></td>
<td align='center'><div align="center">a2b</div></td>
<td align='center'><div align="center">a4a</div></td>
<td align='center'><div align="center">a4b</div></td>
<td align='center'><div align="center">a6a</div></td>
<td align='center'><div align="center">a6b</div></td>
</tr>
<?php
$bgcolor = "";
$content = "";
mysql_data_seek($result, 0);
while($record=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$record['session']."</td>";
echo "<td>".$record['a2a']."</td>";
echo "<td>".$record['a2b']."</td>";
echo "<td>".$record['a4a']."</td>";
echo "<td>".$record['a4b']."</td>";
echo "<td>".$record['a6a']."</td>";
echo "<td>".$record['a6b']."</td>";
echo "</tr>";
}
?>
</table>
请问如何让黄色的avg出现?
谢谢各位大大帮忙
作者: alog (A肉哥)   2014-05-04 13:00:00
无关问题:这Schema有点可怕 a来a去我快吐了http://sqlfiddle.com/ 请贴到 线上测试
楼主: ftseng (F大叔)   2014-05-04 14:47:00
真不好意思!我想要做的是把columns算出平均值,显示在页面但是mysql里面的table没有avg,这样可以吗?
作者: MOONRAKER (㊣牛鹤鳗毛人)   2014-05-04 14:59:00
吐了请你找一个关于avg()的SQL教学网页好好看一下范例好吗AND的用法是谁教你的
楼主: ftseng (F大叔)   2014-05-04 15:21:00
avg(col1+col2)这个是计算出value,但and是想呈现合并表格的状况哈哈! 别笑我了! 我成功了 谢谢大家
作者: MOONRAKER (㊣牛鹤鳗毛人)   2014-05-05 19:40:00
最好SQL写法可以照你[想]的啦

Links booklink

Contact Us: admin [ a t ] ucptt.com