1

My database code is

SELECT `Title`,`Rating`,`Times_Played` from audio WHERE 'Username'='Hamburger7'

There are no entries where the username is equal to Hamburger7, but it selects two entries anyway. Instead of choosing entries that match the condition, it chooses the ones that are equal to '0', (as opposed to NULL).

The output on my PHP admin page, where the server settings are, is:

SELECT  `Title` ,  `Rating` ,  `Times_Played` 
FROM audio
WHERE  `Username` =  'Hamburger7'
LIMIT 0 , 30
4

3 回答 3

2
SELECT `Title`,`Rating`,`Times_Played` from audio WHERE `Username`='Hamburger7'

您将字符串用户名与字符串 Hamburger7 进行比较,而不是列用户名

于 2013-07-15T21:15:56.623 回答
2

我认为问题在于您Username在查询中有单引号,这意味着您的 MySQL 将其视为文字字符串而不是列名。

SELECT `Title`,`Rating`,`Times_Played` from audio WHERE 'Username'='Hamburger7'

将其更改为

SELECT `Title`,`Rating`,`Times_Played` from audio WHERE `Username`='Hamburger7'

或者更好

SELECT Title,Rating,Times_Played from audio WHERE Username='Hamburger7'

因为除非列名是保留字,否则您不需要将列名放在反引号中,并且无论如何使列名与保留字相同是一个坏主意。

于 2013-07-15T21:17:55.553 回答
1

Username列的数据类型是数字类型,可能是 INT。但绝对不是 CHAR 或 VARCHAR。

MySQL 正在将您的字符串文字'Hamburger7'转换为与该列匹配的数据类型。并且在 INT 方面转换为 0。

您的查询实际上是:

WHERE  `Username` = convert_string_literal_to_integer('Hamburger7')

这相当于

WHERE  `Username` = 0

(哦,方便的隐式数据类型转换的乐趣!)

于 2013-07-15T22:32:13.633 回答