将右连接替换为派生表左连接

发布于 2024-11-30 16:54:25 字数 1565 浏览 5 评论 0原文

我如何使用左连接编写这个查询。由于我使用的框架不支持右连接,我需要重写查询。任何人都可以建议我一个可能的解决方案吗?

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
+---------------+-------------+------+-----+---------------------+-------+
| 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 |       |
+---------------+-------------+------+-----+---------------------+-------+

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
+---------------+-------------+------+-----+---------------------+-------+
| 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 |       |
+---------------+-------------+------+-----+---------------------+-------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此生挚爱伱 2024-12-07 16:54:25

只需切换左连接的关系:

在实践中,很少使用显式右外连接,因为它们总是可以用左外连接替换(交换表顺序)并且不提供任何附加功能。

资料来源:Wikipedia.org

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

太傻旳人生 2024-12-07 16:54:25
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);
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);
┊风居住的梦幻卍 2024-12-07 16:54:25

不确定这是否会产生与您现在的输出完全相同的输出,但它可能会给您正确的想法:

select Audit.history_id, Audit.field, History.modifiedtime, History.operation
来自历史
左加入审计 Audit.history_id=History.history_id
其中 History.refid=2000000020088
按历史记录.修改时间排序

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文