我只想知道任何有关性别和种族的想法。数据应显示如下:
-----------------------------
course | Malay | chinese |
| M | F | M | F |
-----------------------------
science | 0 | 1 | 0 | 0 |
-----------------------------
business | 0 | 0 | 1 | 0 |
//这是表结构
$sql = "CREATE TABLE student (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
gender VARCHAR(30) NOT NULL,
race VARCHAR(50),
session VARCHAR(50),
course VARCHAR(50),
)";
示例数据
5273544 |李|男性|中文| june2012 |商业研究
我已有的代码:
<?php
include "connect_db.php";
if ((isset($_POST['submit'])) AND ($_POST['search'] <> "")) {
$search = $_POST['search'];
$sql = "SELECT gender, count(gender) AS cnt FROM student WHERE session LIKE '%$search%' AND gender IN ('Male', 'Female') GROUP BY gender" or die(mysqli_error(
''
));
$result = $conn->query($sql);
while ($row = fetch_assoc($result)) {
$dataSet->addPoint(new Point($row['gender'], $row['cnt']));
}
}
答案 0 :(得分:0)
作为初步想法,您希望生成一个包含所有可能组合的结果集。
理想情况下,你会有一个性别表,一个种族表和一个课程表,然后你可以交叉加入那些以获得所有组合,然后离开加入学生表来获得计数。
如果你没有使用子查询来获取可能的值,那么就会出现这样的情况: -
$sql = "SELECT c.course,
r.race,
g.gender,
COUNT(s.id)
FROM
(
SELECT DISTINCT race
FROM student
) r
CROSS JOIN
(
SELECT DISTINCT gender
FROM student
WHERE gender IN ('Male', 'Female')
) g
CROSS JOIN
(
SELECT DISTINCT course
FROM student
) c
LEFT OUTER JOIN student s
ON r.race = s.race
AND g.gender = s.gender
AND c.course = s.course
AND session LIKE '%".$conn->escape($search)."%'
GROUP BY c.course, r.race, g.gender" or die(mysqli_error(''));
然后只是简单的php将它放在一个表中,当课程改变时推出一个新的表格行。
修改
以下是完全未经测试的,但希望能让您了解如何使用2个标题行回显表: -
<?php
include "connect_db.php";
if ((isset($_POST['submit'])) AND ($_POST['search'] <> ""))
{
$search = $_POST['search'];
$sql = "SELECT c.course,
r.race,
g.gender,
COUNT(s.id) AS res_cnt
FROM
(
SELECT DISTINCT race
FROM student
) r
CROSS JOIN
(
SELECT DISTINCT gender
FROM student
WHERE gender IN ('Male', 'Female')
) g
CROSS JOIN
(
SELECT DISTINCT course
FROM student
) c
LEFT OUTER JOIN student s
ON r.race = s.race
AND g.gender = s.gender
AND c.course = s.course
AND session LIKE '%".$conn->escape($search)."%'
GROUP BY c.course, r.race, g.gender" or die(mysqli_error(
''
));
$result = $conn->query($sql);
echo "<table>";
$header1 = "<tr><td rowspan='2'>course</td>";
$header2 = "<tr>";
$out_row = "<tr>";
$prev_course = '';
$prev_race = '';
$first_row = true;
while ($row = $conn->fetch_assoc($result))
{
if ($prev_course != $row['course'] and $prev_course != '')
{
if ($first_row)
{
echo $header1."</tr>";
echo $header2."</tr>";
$first_row = false;
}
echo $out_row."</tr>";
$out_row = "<tr>";
$prev_course = $row['course'];
}
if ($first_row)
{
if ($prev_race != $row['race'])
{
$header1 .= "<td colspan='2'>".$row['race']."</td>";
$prev_race = $row['race'];
}
$header2 .= "<td>".$row['gender']."</td>";
}
$out_row .= "<td>".$row['res_cnt']."</td>";
}
if ($prev_course != $row['course'] and $prev_course != '')
{
if ($first_row)
{
echo $header1."</tr>";
echo $header2."</tr>";
$first_row = false;
}
echo $out_row."</tr>";
$out_row = "<tr>";
$prev_course = $row['course'];
}
echo "</table>";
}