最好的SQL查询LEFT JOIN,DISTINCT COLUMN,NOT EXIST

时间:2014-05-31 22:35:56

标签: mysql group-by left-join distinct not-exists

我有桌子tbl_actor,tbl_stockholder,tbl_proxy_list

tbl_actor
actor_id, col1, col2

tbl_stockholder
stockholder_id, actor_id, col1, col2

tbl_proxy_list 
proxy_list_id, stockholder_actor_id, proxy_actor_id

我想拥有股东和代理人的主人名单。像这样的东西。

(stockholder_id + proxy_actor_id) - distinct(stockholder_actor_id)

HERES MY QUERY

 SELECT * FROM tbl_actor a
 LEFT JOIN tbl_stockholder s USING (actor_id)
 LEFT JOIN (SELECT * FROM tbl_proxy_list GROUP BY proxy_actor_id) pl ON a.actor_id = pl.proxy_actor_id
 WHERE NOT EXISTS (select null from tbl_proxy_list pl WHERE a.actor_id = pl.stockholder_actor_id) 
 AND (s.stockholder_id IS NOT NULL OR proxy_list_id IS NOT NULL)

这个查询似乎有用..但我觉得他们在这里有一些东西叫(代码嗅觉)。

我在问......有没有比这更好或更合适的东西。

http://sqlfiddle.com/#!2/d70ee7/3

0 个答案:

没有答案