简体   繁体   English

我在 sql 中一起使用 like 和 where 子句时遇到问题

[英]I have problem to use like and where clause together in sql

I want to select data from the SQL table and fetch data from the table with the use of LIKE and BETWEEN clause together and I have tried below query.我想从 SQL 表中获取 select 数据,并使用 LIKE 和 BETWEEN 子句从表中获取数据,我尝试了以下查询。

SELECT * FROM `table_name` WHERE  `date_time` LIKE BETWEEN '%2019-09-20%' AND '%2019-09-29%';

but it shows me the error但它向我显示了错误

You have an error in your SQL syntax;您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BETWEEN '%2019-09-20%' AND '%2019-09-29%' LIMIT 0, 25' at line 1检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的“BETWEEN '%2019-09-20%' AND '%2019-09-29%' LIMIT 0, 25' 附近使用的正确语法

can anybody help me with this.任何人都可以帮我解决这个问题。

You can treat date_time as an actual date.您可以将date_time视为实际日期。

SELECT * FROM `table_name` WHERE STR_TO_DATE(`date_time`, '%Y-%m-%d') BETWEEN STR_TO_DATE('2019-09-20', '%Y-%m-%d') AND STR_TO_DATE('2019-09-29', '%Y-%m-%d')

LIKE is only for comparing strings to a pattern that may contain wildcards; LIKE 仅用于将字符串与可能包含通配符的模式进行比较; Name LIKE 'Smith%' -returns anything starting with Smith. Name LIKE 'Smith%'返回以 Smith 开头的任何内容。

BETWEEN is for comparing numbers or dates to see if they fall into a range. BETWEEN 用于比较数字或日期以查看它们是否属于某个范围。 Between includes the start and end values.之间包括开始值和结束值。 BETWEEN 1 and 5 returns any records that have 1,2,3,4 or 5 BETWEEN 1 and 5返回具有 1、2、3、4 或 5 的任何记录

They cannot be used together, you use one or the other.它们不能一起使用,您可以使用其中一个。

Dates are like decimal numbers;日期就像十进制数字; the time part of a date can mean the date falls outside of the range.日期的时间部分可能意味着日期超出范围。 Just like 5.5 is not "between 1 and 5", a date time of 2019-09-29 12:34 is not "between 2019-09-20 and 2019-09-29" - only midnight on the 29th will fall in the range.就像 5.5 不是“1 和 5 之间”一样,日期时间 2019-09-29 12:34 也不是“2019-09-20 和 2019-09-29 之间”——只有 29 日午夜才会落在范围。 For this reason I tend to prefer using > and < with date ranges:出于这个原因,我倾向于将 > 和 < 与日期范围一起使用:

SELECT * FROM `table_name`
WHERE
  `date_time` >= '2019-09-20' AND
  `date_time` < '2019-09-30'

This includes all records from the 20th to the 29th, no matter what time on the 29th.这包括从 20 日到 29 日的所有记录,无论 29 日是什么时间。 It's more clear and explicit than between.它比介于两者之间更清晰和明确。 The other answer that has BETWEEN... 23:59:59 is also trying to capture everything less than the 30th but it's not such a wise habit to get into, because one day the time may have milliseconds etc BETWEEN... 23:59:59之间的另一个答案也试图捕捉不到 30 日的所有内容,但这并不是一个明智的习惯,因为有一天时间可能只有毫秒等

It's like saying BETWEEN... AND 9.9 because you're looking for values less than 10.. but the.这就像说BETWEEN... AND 9.9因为您正在寻找小于 10.. 但是的值。 One day revere is a value 9.99, and even if you used BETWEEN... AND 9.99 one day there will be 9.999 and so on.一天的敬畏是一个值 9.99,即使你使用BETWEEN... AND 9.99一天也会有 9.999 等等。 If you want "less than 10", use < 10如果您想要“小于 10”,请使用< 10

Try to only use BETWEEN for value ranges you know are discrete尝试仅将 BETWEEN 用于您知道是离散的值范围

LIKE is useless here, you want the rows that date_time between 2019-09-20 and 2019-09-29, probably inclusive. LIKE 在这里没用,你想要 date_time 在 2019-09-20 和 2019-09-29 之间的行,可能包括在内。 so you can use below query, without LIKE所以你可以使用下面的查询,没有 LIKE

SELECT * FROM `table_name` WHERE `date_time` BETWEEN '2019-09-20' AND '2019-09-29 23:59:59'

you can use subquery to get your top matching date.您可以使用subquery来获取最匹配的日期。

select * from `table_name` where `date_time` between 
    (select `date_time` from `table_name` where `date_time` like '%2019-09-20%' limit 1)  and   
    (select `date_time` from `table_name` where `date_time` like '%2019-09-29%' limit 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM