2016-06-21 7 views

Antwort

2

können Sie NOT EXISTS() verwenden:

SELECT * FROM templates t 
WHERE NOT EXISTS(SELECT 1 FROM templates s 
       WHERE s.parent = t.id) 

Oder eine LEFT JOIN:

SELECT t.* FROM templates t 
LEFT JOIN templates s 
ON(t.id = s.parent) 
WHERE s.id is null 

Oder NOT IN():

SELECT * FROM templates t 
WHERE t.id NOT IN(SELECT parent FROM templates) 
+0

hallo, beantworten Sie Job für mich getan hat, aber ich will einen Vorschlag von Ihnen. Wenn ich eine solche Abfrage verwende 'WHERE t.id NOT IN (SELECT übergeordnete FROM-Vorlagen) und status =' 1' funktioniert das nicht, scheint die AND-Klausel nicht mit 'NOT IN' zu funktionieren? – hemsbhardiya

+0

Nein, es gibt kein Problem bei der Kombination von 'NOT IN' mit irgendetwas. Vielleicht etwas mit deinen Daten .. Poste ein Beispiel, wo es nicht funktioniert und ich sage dir warum. @HimanshuBhardiya – sagi

2

Try this;)

select t1.* 
from templates t1 
left join templates t2 on t1.id = t2.parent 
where t2.id is null 

DEMO HERE

2

prüfen dies:

SELECT * FROM `templates` where id NOT IN (SELECT parent FROM `templates` where parent!=0 GROUP BY parent);