使用左连接和连接表的另一个字段中的位置

时间:2012-06-12 21:36:25

标签: mysql sql

我有两个表users和work_orders。 现在我需要通过从users表字段中过滤来获得work_orders结果。

我的以下查询未返回结果。

SELECT `work_orders`.`id` as id, `work_orders`.`type` as type
     , `work_orders`.`title` as title, `work_orders`.`status` as status
     , `work_orders`.`publish_date` as publish_date
     , `work_orders`.`priority` as priority, `work_orders`.`assigned_to` as assigned_to
     , `work_orders`.`client_id` as client_id, `work_orders`.`due_date` as due_date 
  FROM (`work_orders`) 
  LEFT JOIN `users` 
    ON `users`.`id` = `work_orders`.`assigned_to` 
 WHERE `work_orders`.`status` != 'closed' 
   AND users.manager_id = '143' 
 ORDER BY `work_orders`.`id` DESC 
 LIMIT 30

1 个答案:

答案 0 :(得分:0)

您的WHERE子句是否过滤掉所有结果?

此外,如果要显示仅适用于某些用户的work_orders,请将LEFT JOIN更改为INNER JOIN,或使用EXISTS。

试试这个......

SELECT field1, field2, ...
FROM work_orders
WHERE EXISTS (
    SELECT 1 
    FROM users 
    WHERE  users.id = work_orders.assigned_to 
       AND manager_id='143'
)