duandazhen7306 2015-11-28 06:00
浏览 68

SQL选择字符串不起作用

I've already posted this but I want to explain it better. I have a website in which students insert their 4 courses and 4 comments for each course, these are the SQL table columns: Course1, Course2, Course 3, Course 4, Comment1, Comment2, Comment 3, Comment 4.

I have a search in which any student inputs, for instance, geography (which may be saved in any of the four course columns), and I want my SQL query to return all the comments for geography. For example, if a student saved Geography in position Course2, I want my SQL query to select comment2 where course2 = geography. He may have saved it in course1, so it has to be flexible, but only select the course chosen by the student. This is my current SQL query:

$SQL = "SELECT (Comment1 FROM Students WHERE Course1 = 'geography'), (Comment2 FROM Students WHERE Course2 = 'geography'), (Comment3 FROM Students WHERE Course3 = 'geography'),  (Comment4 FROM Students WHERE Course4 = 'geography')";

Currently, this SQL query isn't working. I know the structure may seem odd, but logically, as you may understand, this makes sense, though it's probably not the right way to code it. I then print all the geography comments like this:

$null = '';
if(mysql_num_rows($result)) {
  echo "<ol>";  
 while ($row=mysql_fetch_array($result)) {
  if($row["Class"]!=$null) {  
  if($null!='') {  
  echo "</ol><ol type='1'>";   
  } 
 }

 echo "<li><p>" . " " . $row["Comment1"]. " " . $row["Comment2"]." " .    $row["Comment3"] . " " . $row["Comment4"] . "</li></p>";
$i = $i +1;
}

echo "</ol>";
  • 写回答

3条回答 默认 最新

  • doushe8577 2015-11-28 06:04
    关注

    Your query is wrong: Try this

    SELECT Comment1, Comment2, Comment3, Comment4 
    FROM Students 
    WHERE Course1 = '$value1' AND Course2 = '$value1' 
    AND Course3 = '$value1' AND Course4 = '$value1'
    

    OR

    SELECT Comment1, Comment2, Comment3, Comment4 
    FROM Students 
    WHERE Course1 = '$value1' OR Course2 = '$value1' 
    OR Course3 = '$value1' OR Course4 = '$value1'
    

    Based on your requirement use AND or OR while checking course

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?