我有两张这样的桌子......
表1
+------+----------+-----+--------+
| Year | Runner | Age | Result |
+------+----------+-----+--------+
| 2004 | Runner 1 | 3 | Won |
| 2004 | Runner 2 | 4 | Lost |
| 2004 | Runner 3 | 3 | Lost |
| 2005 | Runner 1 | 3 | Lost |
| 2005 | Runner 2 | 3 | Lost |
| 2005 | Runner 3 | 4 | Won |
| 2006 | Runner 1 | 4 | Won |
| 2006 | Runner 2 | 3 | Lost |
| 2006 | Runner 3 | 4 | Lost |
+------+----------+-----+--------+
表2
+-------+----------+-----+
| RCNum | Runner | Age |
+-------+----------+-----+
| 1 | Runner 1 | 3 |
| 2 | Runner 2 | 4 |
| 3 | Runner 3 | 3 |
| 4 | Runner 4 | 3 |
| 5 | Runner 5 | 3 |
+-------+----------+-----+
现在我将使用......
$numbers = mysql_query("SELECT age,
sum(case when result <> ''then 1 else 0 end) Sec1Picks,
sum(case when result = 'Won' then 1 else 0 end) Sec1Wins
FROM table1
GROUP BY age
ORDER BY age ASC");
while($row = mysql_fetch_array($numbers, MYSQL_ASSOC)) {
echo "(".$row['Age']."yo) ".$row['Sec1Wins']."-".$row['Sec1Picks']; }
哪个会给我年龄,然后是选秀权和获胜者的数量,但我想要做的是在此之后我想要包括那些属于那个因素的RCNum。
所以例如3yo会输出..
(3yo)1-5(1 3 4 5)
(4yo)2-4(2)
我的技能是基本的,所以我不确定如何实现这一点,所以任何帮助将不胜感激!谢谢你的进步!
修改
我目前正在使用......如下所示
SELECT t1.age
, SUM(CASE WHEN t1.result <> '' THEN 1 ELSE 0 END) Sec1Picks
, SUM(CASE WHEN t1.result = 'Won' THEN 1 ELSE 0 END) Sec1Wins
, GROUP_CONCAT(DISTINCT t2.rcnum ORDER BY rcnum SEPARATOR ' ') rcnums
FROM table2 t2
RIGHT
JOIN table1 t1
ON t1.age= t2.age
GROUP
BY t1.age;
这给了我这个......
+-----------+-----------+----------+---------------------------+
| age | Sec1Picks | Sec1Wins | rcnums |
+-----------+-----------+----------+---------------------------+
| 3 | 420 | 14 | 8 9 13 21 22 25 27 |
| 4 | 352 | 24 | 6 7 11 16 17 19 20 29 |
| 5 | 470 | 10 | 1 2 3 5 10 15 18 23 24 26 |
| 6 | 64 | 6 | 12 14 |
| 7 | 25 | 1 | 4 |
| 8 | 7 | 0 | NULL |
| 9 | 2 | 0 | 28 30 |
+-----------+-----------+----------+---------------------------+
但是正在发生的事情是,在rcnums列中插入的每个数字都会在Picks / Wins列中再次添加统计数据!
在Picks / Wins列中,我只想在Table1中计算,因此3yo将是60而2则不是420和14.我将如何更改编码以实现此目的?
答案 0 :(得分:1)
SELECT t2.age
, SUM(CASE WHEN t1.result <> '' THEN 1 ELSE 0 END) Sec1Picks
, SUM(CASE WHEN t1.result = 'Won' THEN 1 ELSE 0 END) Sec1Wins
, GROUP_CONCAT(DISTINCT t2.rcnum ORDER BY rcnum SEPARATOR ' ') rcnums
FROM table2 t2
LEFT
JOIN table1 t1
ON t1.runner = t2.runner
GROUP
BY t2.age;