2008年8月13日 星期三

just sql select, as, relationship

select * from x_alerts as A, alert_targets as B, x_alert_relationships as C where A.status = 'ale' and C.x_alert_id = A.id and C.alert_target_id = B.id and B.user_id = 2590;

這樣加好幾個table 就等於inner join

scope_out :history, :conditions => 'x_alerts.status = "ale"'

XAlert.find_history(:all, :conditions => "id in (select x_alert_id from x_alert_relationships where alert_target_id in (select id from alert_targets where user_id in (#{id})))")

subquery or inner join 誰好 都不一定每種database實作這種東西的方法不同, 自己可以用benchmark測, 不然就是靠知道內部作法的高手 XD


ps:
select id from alert_targets where user_id in (2590);
select id from alert_targets where user_id = 2590;
一樣

沒有留言: