我有这张表
| BookID | BookTitle | NumberOfPages | NoOfCopies |
+--------+--------------------------------+---------------+------------+
| 1 | The Help | 444 | 4 |
| 2 | The Catcher in the Rye | 277 | 10 |
| 3 | Crime and Punishment | 545 | 2 |
| 4 | The Brothers Karamazov | 795 | 1 |
| 5 | A Crown of Wishes | 369 | 12 |
| 6 | The Fireman | 752 | 3 |
| 7 | Fahrenheit 451 | 174 | 9 |
| 8 | The Hobbit | 366 | 1 |
| 9 | Lord of Emperors | 560 | 4 |
| 10 | Holy Bible: King James Version | 1590 | 11 |
----------------------------------------------------------------------------
当我插入书名并希望它返回图书ID时,它总是返回一个空集
到目前为止,我已尝试过这些查询.-> book_info是表格的名称:
select BookID from book_info where ucase(BookTitle) = ' THE HELP% ';
select BookID from book_info where BookTitle = ' The Help ';
select BookID from book_info where lcase(trim(BookTitle) = 'the help';
但他们都没有工作。
注意我在工作中不依赖sql。
答案 0 :(得分:2)
如果你想使用"%"
,你需要使用当你使用" ="你需要确定它是一样的。甚至空间也算数
select BookID from book_info where BookTitle LIKE 'THE HELP%';
答案 1 :(得分:2)
这里的问题在于您正在使用的运算符以及您期望从中运行的值,=
运算符检查完全匹配,这就是您的查询没有返回记录的原因:
select BookID from book_info where ucase(BookTitle) = ' THE HELP% ';
select BookID from book_info where BookTitle = ' The Help ';
select BookID from book_info where lcase(trim(BookTitle) = 'the help';
还有一件事是:
默认情况下,MySQL查询不区分大小写。
因此,您无需在此处添加字符串方法来更改值大小写。
我们通常只使用%
与LIKE
这样:
select BookID from book_info where ucase(BookTitle) LIKE '%THE HELP%';
在此查询中,LIKE %THE HELP%
将匹配其中包含THE HELP
的所有字符串;