Sql在OneToMany关系中找到持续的id

时间:2017-08-04 20:40:01

标签: mysql sql

我只是了解如何获得与此reference关联的最新插入ID,在数据库中我有两个表channel_content_typechannel_plain_text_container,每个channel_plain_text_container channel_content_type content_id idchannel_content_typechannel_content_type引用的行

这意味着channel_plain_text_containerSELECT 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?

此sql命令不正确并且不返回任何行

class HomeViewController: UIViewController {
    override viewDidLoad(){
        super.viewDidLoad()
        let goStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let walkthrough = goStoryboard.instantiateViewController(withIdentifier: "master") as! BWWalkthroughViewController
        let page_one = goStoryboard.instantiateViewController(withIdentifier: "page1") as UIViewController
        let page_two = goStoryboard.instantiateViewController(withIdentifier: "page2")as UIViewController
        let page_three = goStoryboard.instantiateViewController(withIdentifier: "page3")as UIViewController

        // Attach the pages to the master
        walkthrough.delegate = self
        walkthrough.add(viewController:page_one)
        walkthrough.add(viewController:page_two)
        walkthrough.add(viewController:page_three)

        self.present(walkthrough, animated: true, completion: nil)
    }
}

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以按降序对ID进行排序,并获得最上一行:

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对结果进行排序,按降序排列。结果集中的第一行包含您期望的结果。