Sql 在 OneToMany 关系上找到持续的 id [英] Sql find lasted id on OneToMany relation ship

查看:24
本文介绍了Sql 在 OneToMany 关系上找到持续的 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是了解如何获得与此参考相关的最新插入 id,在数据库中我有两个表为 channel_content_typechannel_plain_text_container,每个 channel_plain_text_container 行引用到 channel_content_typecontent_idchannel_content_type

i just learn about how can i get latest inserted id in relation ship with this reference, in database i have two table as channel_content_type and channel_plain_text_container, each channel_plain_text_container row referenced to channel_content_type with content_id column on id of channel_content_type

这意味着 channel_content_typechannel_plain_text_container 上有更多行,现在我如何找到最新插入的 id?

that means channel_content_type have more row on channel_plain_text_container as has many, now how can i find lastest inserted id in that?

这个sql命令不正确,没有返回任何行

this sql command is not correct and return no any row

SELECT ptext.id
FROM channel_content_type content
JOIN channel_plain_text_container ptext ON (content.id = ptext.content_id)
LEFT OUTER JOIN channel_content_type p2 ON (content.id = p2.id)
WHERE p2.id IS NULL

提前致谢

推荐答案

您可以按 Id 按降序对结果进行排序并获得第一行:

You can sort your result by the Id on a descending order and get the top row:

SELECT  ptext.id
FROM channel_content_type content
JOIN channel_plain_text_container ptext ON (content.id = ptext.content_id)
LEFT OUTER JOIN channel_content_type p2 ON (content.id = p2.id)
WHERE p2.id IS NULL
Order by ptext.id desc

在上面的查询中,Order by ptext.id desc 按 id 以降序对结果进行排序.结果集中的第一行包含您期望的结果.

In the above query the Order by ptext.id desc sorts the result by the id, in a descending order. The firs row in the result set contains the result that you expect.

这篇关于Sql 在 OneToMany 关系上找到持续的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆