内连接查询中的where子句

时间:2011-03-26 22:38:58

标签: mysql sql

请帮我解决此问题:

function viewServices($userpno)
{
    echo $userpno;

    $this->query = "
SELECT task.employee_id , task.user_id , task.service_id, service.name AS servicename ,
       service.description AS servicedescription, employee.name AS employeename, employee.pic_path AS employeepicture,
       employee.pic_path 
FROM task where task.user_id = '$userpno' 
INNER JOIN employee ON employee.pno = task.employee_id
INNER JOIN user ON user.pno = task.user_id
INNER JOIN service ON service.service_id = task.service_id
";
}

查询完美无缺:

WHERE task.user_id = '$userpno'

我也试过这种方式:

WHERE task.user_id = $userpno

但它不起作用。

错误是:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\admin\classes\Task.php on line 22

请告诉我如何放置WHERE子句。

4 个答案:

答案 0 :(得分:7)

WHERE子句位于查询的末尾

SELECT task.employee_id , task.user_id , task.service_id, service.name AS servicename ,service.description AS servicedescription, employee.name AS employeename, employee.pic_path AS employeepicture,employee.pic_path
FROM task
INNER JOIN employee  ON employee.pno = task.employee_id 
INNER JOIN user  ON user.pno = task.user_id
INNER JOIN service  ON service.service_id = task.service_id 
where task.user_id = '$userpno'

答案 1 :(得分:3)

查询结构是:SELECT,FROM(此处加入),WHERE

你太早了

$this->query = 
   "SELECT task.employee_id , task.user_id , task.service_id, service.name AS servicename ,service.description AS servicedescription, employee.name AS employeename, employee.pic_path AS employeepicture,employee.pic_path 
    FROM task INNER JOIN employee  ON employee.pno = task.employee_id  INNER JOIN user  ON user.pno = task.user_id INNER JOIN service  ON service.service_id = task.service_id 
    WHERE task.user_id = '$userpno'";

正确运行的查询返回资源,失败的查询返回false

答案 2 :(得分:2)

WHERE子句后面有JOIN子句。这是无效的,因此您的查询返回false,因为它失败了。

作为参考,SELECT查询的部分内容必须符合此处文档中所述的顺序/格式: http://dev.mysql.com/doc/refman/5.0/en/select.html

答案 3 :(得分:2)

尝试:

$this->query = "SELECT task.employee_id , task.user_id , task.service_id,
service.name AS servicename ,service.description AS servicedescription, 
employee.name AS employeename, employee.pic_path AS employeepicture,employee.pic_path
FROM task 
INNER JOIN employee  ON employee.pno = task.employee_id  
INNER JOIN user  ON user.pno = task.user_id 
INNER JOIN service  ON service.service_id = task.service_id 
where task.user_id = '$userpno'";