1

我有两个表用户和工作订单。现在我需要通过从 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
4

1 回答 1

0

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

此外,如果您想显示仅与某些用户相关的工作订单,请将您的 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'
)
于 2012-06-12T21:44:04.633 回答