0

私は次のクエリを使用しています:

SELECT `users`.`id` as id 
FROM (`users`) 
    LEFT JOIN `work_orders` 
        ON `users`.`id` = `work_orders`.`assigned_to` 
WHERE (work_orders.status = 'approved' 
        OR work_orders.status = 'scheduled' 
        OR work_orders.status = 'published') 
    AND work_orders.is_paid = 0 
ORDER BY `users`.`id` DESC 
LIMIT 30

しかし、このクエリでは、レコードが複数usersある場合、それぞれに複数のレコードを取得していますwork_orders。複数あるリストをusers 1回だけリストするにはどうすればよいwork_ordersですか?

4

4 に答える 4

1

DISTINCT句を使用できます。

SELECT DISTINCT(users.id) as id FROM (`users`) LEFT JOIN `work_orders` ON `users`.`id` = `work_orders`.`assigned_to` WHERE (work_orders.status = 'approved' OR work_orders.status = 'scheduled' OR work_orders.status = 'published') AND work_orders.is_paid = 0 ORDER BY `users`.`id` DESC LIMIT 30
于 2012-06-12T19:54:38.617 に答える
0

GROUP BYステートメントを追加して、ユーザーのIDでフィルタリングできます。これは、関心のあるすべてのものだからです。

SELECT `users`.`id` as id 
FROM (`users`) LEFT JOIN `work_orders` ON `users`.`id` = `work_orders`.`assigned_to` 
WHERE (work_orders.status = 'approved' 
  OR work_orders.status = 'scheduled' 
  OR work_orders.status = 'published') 
  AND work_orders.is_paid = 0 
GROUP BY `users`.`id`
ORDER BY `users`.`id` DESC LIMIT 30
于 2012-06-12T19:55:46.853 に答える
0

GROUPBYを使用してみてください

SELECT `users`.`id` as id 
  FROM (`users`) 
  LEFT JOIN `work_orders` 
    ON `users`.`id` = `work_orders`.`assigned_to` 
 WHERE ( work_orders.status = 'approved' 
       OR work_orders.status = 'scheduled' 
       OR work_orders.status = 'published' ) 
   AND work_orders.is_paid = 0 
GROUP BY `users`.`id` DESC 
LIMIT 30
于 2012-06-12T19:55:50.920 に答える
0
SELECT `users`.`id` as id 
  FROM (`users`) 
  WHERE exists ( select 1 from `work_orders` where work_orders.status 
                     in  ('approved' ,'scheduled' ,'published' ) 
                     AND work_orders.is_paid = 0 
                     and `work_orders`.`assigned_to` =`users`.`id` )
 ORDER BY `users`.`id` DESC 
 LIMIT 30
于 2012-06-12T19:58:58.723 に答える