我有这个问题
SELECT `version` FROM filescontent WHERE `fileID`=(SELECT `fileID` FROM files WHERE `fileName`= 'file1.jpg') ORDER BY `fileID` DESC LIMIT 1
问题在于它没有按顺序降序。结果是在数据库中找到的第一个是' 1'。
答案 0 :(得分:1)
原因是因为你正在使用LIMIT 1.因此,如果有1条记录,你怎么能指望它在下降。
您可以尝试删除LIMIT 1,然后检查
SELECT `version` FROM filescontent
WHERE `fileID`=(SELECT `fileID` FROM files WHERE `fileName`= 'file1.jpg')
ORDER BY `fileID` DESC
同样,正如Hearner建议您可以尝试使用JOIN链接表格,如下所示:
SELECT `version`
FROM filescontent join files using(fileID)
WHERE `fileName`= 'file1.jpg'
ORDER BY `fileID` DESC
修改强>
SELECT version FROM filescontent
WHERE fileID=(SELECT fileID FROM files
WHERE fileName= 'file1.jpg')
ORDER BY version DESC LIMIT 1
答案 1 :(得分:1)
使用以下查询 -
SELECT version
FROM filescontent
WHERE fileID=
(
SELECT fileID
FROM files WHERE fileName= file1.jpg
)
ORDER BY version DESC LIMIT 1
更好的查询将是 -
SELECT `version`
FROM filescontent fc join files fl on fl.fileid=fc.fileid
WHERE `fileName`= 'file1.jpg'
ORDER BY `version` DESC limit 1;