简体   繁体   中英

using left join and where in another field of joined table

I have two tables users and work_orders. Now I need to get work_orders results by filtering from users table field.

My following query did not return result.

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

Is your WHERE clause filtering out all results?

Also, if you want to display work_orders that only pertain to certain users, change your LEFT JOIN to an INNER JOIN, or use EXISTS.

Try this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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