【发布时间】:2015-08-05 10:53:50
【问题描述】:
我在如何使用 were 和 like 语句从数据库表中选择数据时遇到问题。
$hy=mysql_query("select (Total) AS firstterm
FROM studentmark, subject
where studentmark.student_id='$name'
AND studentmark.YEAR='$ya'
AND subject.code=studentmark.code
AND studentmark.TERM='$term' LIKE 'F%'");
$hm=mysql_num_rows($hy);
$fetch=mysql_fetch_array($hy);
echo $fetch['firstterm'];
问题是表中未选择具有 89 作为 Total 的术语中的 LIKE 'F%' (FIRST),但选择了具有 73 的术语中的 LIKE 'S%' (SECOND)。有什么我遗漏的吗?
下表
TERM | CODE |student_id|contAss20Asg|ClassWk10 |Test2nd10|YEAR |EXAM| TOTAL
FIRST | AGR | John | 18 |5 | 7 |2011 | 59 | 89
SECOND |AGR2 |John | 13 |6 | 4 |2011 | 40 | 73
THIRD |AGR3 |John | 18 |6 | 8 |2011 | 34 | 64
FIRST |BIO |John | 12 |3 | 3 |2011 | 55 | 73
SECOND |BIO2 |John | 14 |8 | 7 |2011 | 56 | 85
THIRD |BIO3 |John | 12 |8 | 8 |2011 | 42 | 70
我的代码如下所述
<?php echo '</td><td>'?>
<?php
if ($fetch['Total']==NULL){
echo 'missed';
}else
$hy=mysql_query("select (Total) AS secondterm FROM studentmark, subject where studentmark.student_id='$name' AND studentmark.YEAR='$ya' AND subject.code=studentmark.code AND studentmark.TERM='$term' LIKE 'S%'");
$hm=mysql_num_rows($hy);
$fetch=mysql_fetch_array($hy);
echo $fetch['secondterm'];
?>
<?php echo '</td><td>'?>
<?php
if ($fetch['Total']==NULL){
}else
$hy=mysql_query("select (Total) AS firstterm FROM studentmark, subject where studentmark.student_id='$name' AND studentmark.YEAR='$ya' AND subject.code=studentmark.code AND studentmark.TERM='$term' LIKE 'F%'");
$hm=mysql_num_rows($hx);
$hm=mysql_num_rows($hy);
$row=mysql_fetch_array($hy);
echo $row['secondterm'];
?>
<?php echo '</td><td>'?>
<?php
if ($fetch['Total']==NULL){
//echo 'missed';
}else
$hy=mysql_query("select (Total) AS thirdterm FROM studentmark, subject where studentmark.student_id='$name' AND studentmark.YEAR='$ya' AND subject.code=studentmark.code AND studentmark.TERM='$term'");
$hm=mysql_num_rows($hy);
$hm=mysql_num_rows($hy);
$fetch=mysql_fetch_array($hy);
$row=mysql_fetch_array($hy);
echo $fetch['firstterm']+ $row['secondterm'] + $fetch['thirdterm'];
?>
【问题讨论】:
-
对这些答案有任何反馈吗?还是你只是吃着吃着跑着?
标签: php mysql select where sql-like