简体   繁体   English

使用 sql 查询结果作为新 sql 语句的搜索词

[英]Using an sql query result as a search term for a new sql statement

So I am trying to use a result from an SQL search $search_course and then having that as the WHERE LIKE part of the next statement $Search_Module .所以我试图使用 SQL 搜索$search_course ,然后将其作为下一个语句$Search_ModuleWHERE LIKE部分。

once the $Search_Module query has finished I would like the results to be in a list which I know should be coded correctly.一旦$Search_Module查询完成,我希望结果出现在我知道应该正确编码的列表中。

I have tried the following code but the search returns no values;我尝试了以下代码,但搜索没有返回任何值;

Any help with this would be much appreciated, thanks in advance.任何与此有关的帮助将不胜感激,在此先感谢。

        $search_course = "
    SELECT title, id, wyl, overview, module, course, careers
    FROM course
    LEFT JOIN cm
    ON course.id=cm.course
    WHERE id LIKE '%".$_POST['submitcourseselection']."%'";

    $result = $mysqli->query($search_course) or die($mysqli->error);
    $display_course = $result->fetch_assoc();

    //Searches the module table in the DB for modules within the course
    $Search_Module = "
    SELECT id, title, level, credits
    FROM module
    WHERE id LIKE '".$search_course['module']."'";

    $M_Results = $mysqli->query($Search_Module) or die($mysqli->error);


    $ModuleList = '';
    while ($MResults = $M_Results->fetch_assoc()) {
        $ID = $MResults['id'];
        $Title = $MResults['title'];
        $Level = $MResults['level'];
        $Credits = $MResults['credits'];
        $ModuleList.='<div><h2>'.$Title.'</h2></div>'; 
    }

you don't need LIKE in your second query and you can do all by one query.您在第二个查询中不需要 LIKE,您可以通过一个查询完成所有操作。

    $search_course = "
        SELECT id, title, level, credits
        FROM module
        WHERE id IN (SELECT module
        FROM course
        LEFT JOIN cm
        ON course.id=cm.course
        WHERE id LIKE '%".$_POST['submitcourseselection']."%'")";

in your where clause WHERE id LIKE '".$search_course['module']."'"; , you are referencing an array location of a string $search_course , which is your SQL statement.在您的 where 子句WHERE id LIKE '".$search_course['module']."'"; ,您正在引用字符串$search_course的数组位置,这是您的 SQL 语句。

I think you meant to reference your result array $display_course , so try this:我想你的意思是引用你的结果数组$display_course ,所以试试这个:

$search_course = "
SELECT title, id, wyl, overview, module, course, careers
FROM course
LEFT JOIN cm
ON course.id=cm.course
WHERE id LIKE '%".$_POST['submitcourseselection']."%'";

$result = $mysqli->query($search_course) or die($mysqli->error);
$display_course = $result->fetch_assoc();

//Searches the module table in the DB for modules within the course
$Search_Module = "
SELECT id, title, level, credits
FROM module
WHERE id LIKE '".$display_course['module']."'";

$M_Results = $mysqli->query($Search_Module) or die($mysqli->error);


$ModuleList = '';
while ($MResults = $M_Results->fetch_assoc()) {
    $ID = $MResults['id'];
    $Title = $MResults['title'];
    $Level = $MResults['level'];
    $Credits = $MResults['credits'];
    $ModuleList.='<div><h2>'.$Title.'</h2></div>'; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM