SQL“类似”声明:“杰夫”像“杰夫”一样= 假?
编辑:问题已解决。是我的阅读不正确,而不是 SQL :p
嗨!
我刚刚在我的数据库书中读到有关 LIKE SQL 语句的内容。它是这样说的:
SELECT whatever FROM whereever WHERE 'Jeff' LIKE 'Jeff';
它继续说语句“Jeff”LIKE“Jeff”将始终返回 false。书上没有告诉我为什么,我在其他地方也找不到这个。那么这是否意味着下面的 SQL 也会返回 null?
SELECT W.name FROM whereever W WHERE w.name LIKE 'Jeff';
作为参考,这本书是: 数据库管理系统:Ramakrishnan - Gehrke 来自 McGRAW - Hill。国际版。 ISBN 0-07-123151-X 第 140 页。
EDIT: Problem solved. It was my reading that was incorrect, not the SQL :p
Hi!
I was just reading in my database book about the LIKE SQL statement. It said this:
SELECT whatever FROM whereever WHERE 'Jeff' LIKE 'Jeff';
It continued to say that the statement 'Jeff' LIKE 'Jeff' would always return false. The book did not tell me why, nor can I find this anywhere else. Does this then mean that the following SQL also would return null?
SELECT W.name FROM whereever W WHERE w.name LIKE 'Jeff';
For reference, the book is:
Database Management Systems: Ramakrishnan - Gehrke From McGRAW - Hill. International edition. ISBN 0-07-123151-X PAGE 140.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我看了一下那个页面(亚马逊“在这本书中搜索”)你错过的关键是作者在那里提出了关于空白的观点。这本书实际上说
注意空格!
只是为了弄清楚发生错误的原因,以下是文本:
由于空格位于线,很难看到。但正如我下面所说,如果没有空格,引号不会换行到下一行。
I took a look at that page (Amazon "search inside this book") and the key thing that you're missing is that the author is making a point there about whitespace. The book actually says
Note the spaces!
Just to make it clear why the mistake occurred, here's the text:
Since the space is at the end of the line, it's hard to see. But as I say below, the quote mark wouldn't wrap to the next line if there were no space there.
MySQL 不同意你的书(不确定其他 DBMS):
MySQL would disagree with your book (not sure about other DBMSes):
不知道为什么它会说-“杰夫”就像“杰夫”一样是完全正确的。在这种情况下,它将返回“whereever”中的所有行。
在第二个语句中,它将仅返回“name”列恰好是“Jeff”的行。如果是“Jeff Something”,则不会返回。如果你想返回类似的东西,那么你必须做类似“w.name like 'Jeff%'”的事情。还有更多的变体,我相信您在阅读更多内容时会发现......
Not sure why it would say that - 'Jeff' like 'Jeff' is exactly true. In which case, it will return ALL the rows from 'whereever'
In your second statement, it will only return rows where the the 'name' column is exactly 'Jeff'. If it is 'Jeff something', it won't return. If you want to return something like that, then you have to do something like 'w.name like 'Jeff%'. There are many more variations of this, which I'm sure you'll discover as you read on more...
它可能取决于 DBMS 实现,但如果您运行“select 'jeff' like 'jeff';”从 MySQL 中它返回“1”(也称为 true)。
It could depend on the DBMS implementation, but if you run "select 'jeff' like 'jeff';" from MySQL it return "1" (aka true).
完全错误。以下语句返回“Y”(因为条件评估为 true:
...而此语句不返回任何行(因为它评估为 false):
我不知道这些作者在想什么,但没有任何通配符的 LIKE 比较是完全可以接受;在语义上比较左侧和右侧以确保完全相等。
Totally false. The following statement returns 'Y' (because the condition evaluates to true:
... while this statement returns no rows (because it evaluates to false):
I don't know what these authors were thinking, but LIKE comparisons without any wildcards are perfectly acceptable; the left and right sides are compared semantically for exact equality.
本书的这一部分涉及空白。他们说,在 sql 中,
'Jeff' = 'Jeff ' (注意额外的空格)结果为 TRUE,但
'Jeff' LIKE 'Jeff ' (再次,带有额外的空格)结果为 FALSE
希望这能解决问题。
That section of the book deals with blanks. They are saying that, in sql,
'Jeff' = 'Jeff ' (note the extra space) results in TRUE but
'Jeff' LIKE 'Jeff ' (again, with the extra space) results in FALSE
Hope that clears things up.