PHP | MySql:mysql查询concat in where子句

时间:2016-01-17 11:38:31

标签: php mysql

如何在where子句

中连接两列

这是我的常规查询:

$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());

我希望将stud_fname和stud_lname连接起来。

这是我的完整代码:

<?php   
error_reporting(0);
$link = mysql_pconnect("localhost", "root", "") or die("Could not connect");
mysql_select_db("dbmobile_class_record") or die("Could not select database");

// array for JSON response
$response = array();

$instructor_id=$_GET['instructor_id'];
$description=$_GET['description'];
$stud_fname=$_GET['stud_fname'];
$stud_lname=$_GET['stud_lname'];
// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());

if (mysql_num_rows($result) > 0) {

    $response["student"] = array();

    while ($row = mysql_fetch_array($result)) {
            // temp user array
            $student = array();
            $student["stud_id"] = $row["stud_id"];

            // push ordered items into response array
            array_push($response["student"], $student);
           }
      // success
     $response["success"] = 1;
}
else {
    // order is empty
      $response["success"] = 0;
      $response["message"] = "No Records Found";
}
// echoing JSON response
echo json_encode($response);

?>

2 个答案:

答案 0 :(得分:2)

$instructor_id = $_GET['instructor_id'];
$description = $_GET['description'];
$fullname = $_GET['fullname'];

// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND CONCAT(stud_fname, ' ', stud_lname) = '$fullname'") or die(mysql_error());

答案 1 :(得分:0)

使用此

$result = mysql_query("SELECT stud_id, CONCAT_WS('', stud_fname, stud_lname) AS stud_fullname FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());

stud_fullname应包含您想要的内容。