开发者

MySql query (joins)

开发者 https://www.devze.com 2023-03-14 02:13 出处:网络
I have table \"item\" with columns:: id(primary_key)|parent_id|name| 开发者_Python百科 I need to get all items that not have children.SELECT [some cols]

I have table "item" with columns::

|id(primary_key)|parent_id|name|
开发者_Python百科

I need to get all items that not have children.


SELECT [some cols]
  FROM item i
       LEFT JOIN item children ON children.parent_id = i.id
 WHERE children.id IS NULL;


SELECT * FROM item WHERE id NOT IN (SELECT parent_id FROM item)

There are other ways to write this query that may be more performant. But this should get you started.

It will select all records with an id that is not found in the parent_id column - meaning this record does not have children.

0

精彩评论

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