2014-10-03 189 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)