开发者

Replacing right join with derived table to left join

开发者 https://www.devze.com 2023-03-29 01:46 出处:网络
How do i write this query, with left join. since the framework i use doesn\'t support right join i need to rewrite the query. Can any one suggest me a possible solution.

How do i write this query, with left join. since the framework i use doesn't support right join i need to rewrite the query. Can any one suggest me a possible solution.

select Audit.history_id,Audit.field,modifiedtime,operation from Audit right join (select History.history_id from History where refid=2000000020088 order by
modifiedtime limit 5) as Hist on Audit.history_id=Hist.history_id;

    desc Audit
    +------------------+------------+------+-----+---------+-------+
    | Field            | Type       | Null | Key | Default | Extra |
    +------------------+------------+------+-----+---------+-------+
    | AUDIT_ID         | bigint(19) |      | PRI | 0       |       |
    | HISTORY_ID       | bigint(19) |      | MUL | 0       |       |
    | FIELD            | varchar(50)       |     |         |       |
    | OLD_VALUE        | varchar(50)| YES  |     | NULL    |       |
    | NEW_VALUE        | varchar(50)| YES  |     | NULL    |       |
    +------------------+------------+------+-----+---------+-------+

desc History
+---------------+-------------+------+-----+---------------------+-----开发者_如何学JAVA--+
| Field         | Type        | Null | Key | Default             | Extra |
+---------------+-------------+------+-----+---------------------+-------+
| HISTORY_ID    | bigint(19)  |      | PRI | 0                   |       |
| REFID         | bigint(19)  |      | MUL | 0                   |       |
| OPERATION     | varchar(50) | YES  |     | NULL                |       |
| MODIFIED_TIME | datetime    |      |     | 0000-00-00 00:00:00 |       |
+---------------+-------------+------+-----+---------------------+-------+


Simply switch the relations for a left join:

In practice, explicit right outer joins are rarely used, since they can always be replaced with left outer joins (with the table order switched) and provide no additional functionality.

Source:Wikipedia.org


select 
    Audit.history_id,Audit.field,modifiedtime,operation 
from    
    (
        select History.history_id 
        from History where refid=2000000020088 
        order by modifiedtime limit 5            
    ) as Hist 
    left join Audit on (Audit.history_id = Hist.history_id);


Not sure if this will produce exactly same output as the one you have now, but it might give you the right idea:

select Audit.history_id, Audit.field, History.modifiedtime, History.operation
from History
left join Audit on Audit.history_id=History.history_id
where History.refid=2000000020088
order by History.modifiedtime

0

精彩评论

暂无评论...
验证码 换一张
取 消