【问题标题】:How to select a data from a table using the where and like statement and echo it如何使用 where 和 like 语句从表中选择数据并回显
【发布时间】: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


【解决方案1】:

LIKE 是用于比较的运算符,您需要将其用作运算符,就像使用 '='、'>' 等一样。

studentmark.TERM LIKE '%someval%';

希望能回答你的问题。

【讨论】:

    【解决方案2】:

    like 语法错误,试试这个

    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'");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多