如何使用where和like语句从表中选择数据并回显它

时间:2015-08-05 10:53:51

标签: php mysql select where sql-like

我遇到了如何使用are和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为总计的项中的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'];

?>

2 个答案:

答案 0 :(得分:0)

LIKE是一个用于比较的运算符,您需要将它用作运算符,就像使用'=','&gt;'一样等

studentmark.TERM LIKE '%someval%';

希望能回答你的问题。

答案 1 :(得分:0)

类似的语法错了所以试试这个

SELECT TOTAL AS firstterm 
FROM studentmark, subject 
WHERE studentmark.student_id='$name' 
  AND subject.code=studentmark.code   
  AND studentmark.YEAR='$ya' 
  AND studentmark.TERM LIKE 'F%'");

使用JOIN语法也可以更好地编码,就像这样

SELECT TOTAL AS firstterm 
FROM studentmark
   JOIN subject ON subject.code=studentmark.code
WHERE studentmark.student_id='$name' 
  AND studentmark.YEAR='$ya' 
  AND studentmark.TERM LIKE 'F%'");

如果他们发明了一个FOURTH术语会发生什么,这将无法产生正确的答案。由于您的TERM列似乎已形式化为FIRSTSECONDTHIRD,因此最好完全松开LIKE并执行

SELECT TOTAL AS firstterm 
FROM studentmark
   JOIN subject ON subject.code=studentmark.code
WHERE studentmark.student_id='$name' 
  AND studentmark.YEAR='$ya' 
  AND studentmark.TERM = 'FIRST'");