MySQL 通配符“=” - 有没有一个
所以,
SELECT * FROM table WHERE col LIKE '%'
会归还一切。 查询是否有通配符
SELECT * FROM table WHERE col = '*'
显然 *
不起作用,我只是将其放在那里以指示我想要通配符的位置。 我选择的列包含 1 到 12 之间的整数,并且我希望能够选择具有特定数字的所有记录,或具有通配符的所有记录。
谢谢,
So,
SELECT * FROM table WHERE col LIKE '%'
will return everything. Is there a wildcard for the query
SELECT * FROM table WHERE col = '*'
Clearly *
doesn't work, I just put it there to indicate where I'd like a wildcard. The column I'm selecting from contains an integer between 1 and 12, and I want to be able to select either all records with a particular number, or all records with a wildcard.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
LIKE 基本上与 = 相同,只是 LIKE 允许您使用通配符。
这两个查询将返回相同的结果:
LIKE 查询中没有“%”,它实际上与“=”相同。
如果要对整数列进行选择,则应考虑使用 IN() 或 BETWEEN 运算符。 听起来您有两个单独的条件应该在代码中处理,而不是在查询中处理,因为您的条件表明您需要至少两种不同类型的查询。
编辑:我应该澄清 LIKE 和 = 仅在正常、单调的字符串比较用法中相似。 您应该查看 MySQL 手册 了解有关的详细信息它是如何工作的,因为在某些情况下它是不一样的(例如语言集)。
LIKE is basically the same as =, except LIKE lets you use wildcards.
These two queries will return the same results:
Without a '%' in the LIKE query, it is effectively the same as '='.
If you're doing a selection on an integer column, you should consider using the IN() or BETWEEN operators. It sounds like you have two separate conditions that should be handled in your code however, rather than in the query, as your conditions dictate that you need at least two different kinds of queries.
Edit: I should clarify that LIKE and = are similar only in normal, humdrum string comparison usage. You should check the MySQL Manual for specifics on how it works, as there are situations where it's not the same (such as language sets).
如果您想选择所有内容,为什么要附加 WHERE 子句呢? 只需有条件地将其保留,而不是在其中添加通配符。
If you want to select everything, why are you attaching the WHERE clause at all? Just leave it off conditionally instead of putting a wildcard into it.
使用 LIKE 的原因是 = 不提供通配符支持。 不然就没理由LIKE
The reason for using LIKE is because the = does not offer wildcard support. Otherwise there would be no reason for LIKE
即正则表达式 LIKE。
i.e. regular-expression LIKE.
zombat的答案很好,但我只在他的答案中注意到你正在选择整数。 他提到了 IN() 和 BETWEEN()。 以下是使用这些语法的示例,以及整数字段的一些其他选项。
zombat's answer is great, but I only noticed in his answer that you are selecting integers. He mentioned IN() and BETWEEN(). Here's examples using those syntaxes, as well as some other options you have for an integer field.
假设您的查询是参数驱动的,则 case 语句可能是合适的,
如果您不需要值,则
@myvariable
为 null,否则它将使用您传入的整数值。Assuming your query is parameter driven a case statement is probably appropriate
Where
@myvariable
is either null if you dont want a value otherwise it would use the integer value you pass in.我在为报表构建存储过程时遇到过这样的情况
以下是我的解决方案,希望这就是您的想法:)
查询:
I have encountered such a case while building a stored procedure for a report
Following is my solution, hope this is what you had in mind :)
Query:
如果您的值在 (1,12) 范围内,则:
select * from table where col>=5 and col<=5; //这等于 col=5
select * from table where col>=0 and col<=12; //this 等于 col=any value
通过适当选择 2 个参数,同一行可以产生两种效果。
当我需要一个准备好的语句时,我遇到了类似的问题,该语句应该以两种不同的方式工作,要么检查列中的特定值,要么完全忽略该列。
If your values are in the the range (1,12) then:
select * from table where col>=5 and col<=5; //this is equal to col=5
select * from table where col>=0 and col<=12; //this is equal to col=any value
The same line can produce both effects by choosing the 2 parameters appropriately.
I faced a similar problem when I needed a single prepared statement which should work with 2 different ways , either checking for a particular value in a column or ignoring that column completely.