[英]How to select a data from a table using the where and like statement and echo it
我有一個關於如何使用are and 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'];
?>
LIKE是一個比較運算符,您需要像使用'=','>'等一樣將其用作運算符。
studentmark.TERM LIKE '%someval%';
希望這能回答你的問題。
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專欄似乎已正式定義為FIRST
, SECOND
, THIRD
因此最好完全放松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'");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.