2014-10-03 274 views
0

//編輯發現問題。我將所有類型從文本更改爲varchar。現在它工作正常。SQL連接返回none值

我有一個名爲 「聲音」 表看起來像這樣:

ROWID類型爲int(11)| userID type text | name type text | soundfile type text
1 | 「mod001:02」| 「吉米」| 「音樂/ Song.mp3的」

和一張桌子稱爲 「soundlist」 它看起來像這樣:

soundID類型爲int(11)| name type text | soundfile type text | 已使用類型tinyint(1)
1 | 「topSong」| 「music/song.mp3」| 1個

我的問題是,當我運行此查詢

SELECT * 
FROM sounds 
INNER JOIN soundlist 
ON STRCMP(soundlist.soundfile, sounds.soundfile) = 0 
WHERE STRCMP(sounds.userID, "mod001:02") = 0; 

我得到一個空的結果!

我的目標是將「soundlist.used」設置爲0.我只給出了「sounds.userID」。 我目前使用此查詢:

UPDATE soundlist 
INNER JOIN sounds 
ON STRCMP(sounds.userID, "mod001:02") = 0 
SET soundlist.used = 0 
WHERE STRCMP(soundlist.soundfile, sounds.soundfile) = 0; 
+0

只是使用更新,如果你想改變單個表的值??? – 2014-10-03 13:38:53

+2

爲什麼你不加入rowid = soundid – Mihai 2014-10-03 13:38:53

+0

你使用的是什麼版本的MySQL? – Mureinik 2014-10-03 13:41:42

回答

0

編輯:這將更新sounds.userid爲 「0」,其中soundlist.used是 「1」

UPDATE sounds 
INNER JOIN soundlist ON 
    sounds.soundfile = soundlist.soundfile 
SET sounds.userid = "0" 
WHERE soundlist.used = "1" 

如果不是你想要的聲音.userid等於soundlist.us

UPDATE sounds 
INNER JOIN soundlist ON 
    sounds.soundfile = soundlist.soundfile 
SET sounds.userid = soundlist.used 
+0

但聲音沒有「userid」字段。 – 2014-10-03 13:48:32

0

您可以使用嵌套查詢:

UPDATE soundlist 
    set soundlist.used=0 
    where soundfile IN (    -- using IN keyword instead of = if you want to update multiple entries 
     select sounds.soundfile 
      from sounds 
      where sounds.rowID=1  -- or any other condition 
    ); 

我假設rowID是一個INT。

如果你想更進一步,不要打擾比較字符串,爲什麼不使用外鍵?

讓聲音以同樣的方式:

rowID | userID  | name | soundfile 
    1 | "mod001:02" | "Jimmy" | "music/song.mp3" 

並修改聲音列表引用的聲音:

soundID | name  | soundId | used 
     1 | "topSong" | 1   | 1 

您的疑問:

SELECT * 
FROM sounds 
INNER JOIN soundlist 
ON STRCMP(soundlist.soundfile, sounds.soundfile) = 0 
WHERE STRCMP(sounds.userID, "mod001:02") = 0; 

將成爲

SELECT * 
FROM sounds s 
INNER JOIN soundlist l 
ON s.rowId=l.soundId 
where STRCMP(s.userID, "mod001:02") = 0; 

這可以爲您節省一個STRCMP。

考慮使用用於條件對VARCHAR列的索引,它更快,有時更容易閱讀查詢(s.userID =「mod001:02」更straigthforward)