2013-03-29 64 views
0

我想要查詢一個數據庫的節點,即沒有關係又名外鍵。我試過這個加入聲明,但它沒有工作Mysql語句返回沒有外鍵的主鍵?

SELECT nodes.*, relationships.* FROM nodes inner JOIN relationships ON nodes.id = null; 

我怎樣才能只顯示沒有關係的節點?謝謝

nodes     Relationships      
-----     ------------- 
id int(11),   id int(11), 
name varchar(35),  to int(11), //this is the destination node from the id relation 
color varchar(7),  data varchar(0) null 
type varchar (12), Foreign key (id) references nodes(id) 
Primary key (id)  

engine = innodb  

回答

2

您的問題是ON nodes.id = null你必須ON語句一起使用的列名。如果你想設置nodes.id = null,用它where

SELECT 
    nodes.* 
FROM 
    nodes left JOIN relationships ON nodes.id = Relationships.id 
where 
    Relationships.id is null; 
+0

是的空是不是一個好主意,因爲它返回一個空集,我怎麼能查詢這個數據庫來顯示我沒有任何外鍵的節點? – user1902588

+0

編輯我的答案,看看 –

+0

是的,它一直告訴我這個錯誤#1064 - 你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行 relationships.id爲null處使用正確的語法 – user1902588

0
SELECT * FROM nodes WHERE id NOT IN (SELECT `to` from Relationships); 

這將返回不必須在關係表

也是一個ID節點最好的做法是隻在你的數據庫中出現使用小寫有些數據庫區分大小寫,其他數據庫則不區分

+0

嘿joel我在做查詢時實際上遇到錯誤,這是什麼意思? #1241 - 操作數應該包含1列 – user1902588

+0

use \'to \'因爲是mysql中的保留字。 –

+0

#1054 - 未知列'到'在'字段列表' – user1902588