MySQL 通配符“=” - 有没有一个

发布于 2024-07-18 10:29:52 字数 288 浏览 4 评论 0原文

所以,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

酒与心事 2024-07-25 10:29:52

LIKE 基本上与 = 相同,只是 LIKE 允许您使用通配符。

这两个查询将返回相同的结果:

SELECT * FROM table WHERE col LIKE 'xyz';
SELECT * FROM table WHERE col='xyz';

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:

SELECT * FROM table WHERE col LIKE 'xyz';
SELECT * FROM table WHERE col='xyz';

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).

栀子花开つ 2024-07-25 10:29:52

如果您想选择所有内容,为什么要附加 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.

悲念泪 2024-07-25 10:29:52

使用 LIKE 的原因是 = 不提供通配符支持。 不然就没理由LIKE

The reason for using LIKE is because the = does not offer wildcard support. Otherwise there would be no reason for LIKE

最单纯的乌龟 2024-07-25 10:29:52
SELECT * FROM table WHERE col RLIKE '.*'

即正则表达式 LIKE。

SELECT * FROM table WHERE col RLIKE '.*'

i.e. regular-expression LIKE.

凡间太子 2024-07-25 10:29:52

zombat的答案很好,但我只在他的答案中注意到你正在选择整数。 他提到了 IN() 和 BETWEEN()。 以下是使用这些语法的示例,以及整数字段的一些其他选项。

SELECT * FROM table WHERE col = 1;
SELECT * FROM table WHERE col BETWEEN 1 AND 12;
SELECT * FROM table WHERE col BETWEEN 6 AND 12;
SELECT * FROM table WHERE col <= 6;
SELECT * FROM table WHERE col < 6;
SELECT * FROM table WHERE col >= 6;
SELECT * FROM table WHERE col > 6;
SELECT * FROM table WHERE col <> 6;
SELECT * FROM table WHERE col IN (1,2,5,6,10);
SELECT * FROM table WHERE col NOT IN (1,2,5,6,10);

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.

SELECT * FROM table WHERE col = 1;
SELECT * FROM table WHERE col BETWEEN 1 AND 12;
SELECT * FROM table WHERE col BETWEEN 6 AND 12;
SELECT * FROM table WHERE col <= 6;
SELECT * FROM table WHERE col < 6;
SELECT * FROM table WHERE col >= 6;
SELECT * FROM table WHERE col > 6;
SELECT * FROM table WHERE col <> 6;
SELECT * FROM table WHERE col IN (1,2,5,6,10);
SELECT * FROM table WHERE col NOT IN (1,2,5,6,10);
冷夜 2024-07-25 10:29:52

假设您的查询是参数驱动的,则 case 语句可能是合适的,

select * from mytable
where col like case when @myvariable is null then % else myvariable end

如果您不需要值,则 @myvariable 为 null,否则它将使用您传入的整数值。

Assuming your query is parameter driven a case statement is probably appropriate

select * from mytable
where col like case when @myvariable is null then % else myvariable end

Where @myvariable is either null if you dont want a value otherwise it would use the integer value you pass in.

瞎闹 2024-07-25 10:29:52

我在为报表构建存储过程时遇到过这样的情况
以下是我的解决方案,希望这就是您的想法:)

set @p = "ALL";

查询:

select * from fact_orders
where
dim_country_id = if(@p is null or @p="ALL", dim_country_id, @p)
limit 10
;

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 :)

set @p = "ALL";

Query:

select * from fact_orders
where
dim_country_id = if(@p is null or @p="ALL", dim_country_id, @p)
limit 10
;
初雪 2024-07-25 10:29:52

如果您的值在 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文