可以同时使用LIKE和AND吗?

huangapple go评论45阅读模式
英文:

can you use LIKE and AND together

问题

我正在尝试获取在2016年和2018年下单的所有订单:

SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate LIKE '2016%' OR OrderDate LIKE '2018%';

但它只显示2016年的结果。

我尝试了以下查询:

SELECT OrderID, OrderDate
FROM Orders
WHERE YEAR(OrderDate) = 2016;

但它返回一个空白屏幕,我理解这是因为没有一行的值等于'2016-%m-%d'。

我还尝试了使用'IN',结果与'='相同。在这一点上,我尝试了不同的运算符,但结果都一样,要么出现错误,要么是一个空白屏幕。

有人知道我做错了什么吗?

英文:

I'm trying to get all the orders that have been placed on 2016 and on 2018

SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate LIKE '2016%' AND '2018%';

But it only show the results of 2016

I tried

SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate = date('2016-%m-%d');

But it returns a empty screen, I understand that's because there isn't a row with a value of '2016-%m-%d'

I also tried with 'IN' and its the same result as '=' At this point I have tried with different operator but the results are the same. either error or a white screen.

Does anyone know what am I doing wrong?

答案1

得分: 2

你必须在AND的两边重复整个表达式:

WHERE OrderDate LIKE '2016%' AND OrderDate LIKE '2018%';

但这对于任何值来说都不可能成立。没有东西可以同时以20162018开头。也许你想要用OR代替AND

英文:

You have to repeat the whole expression on both sides of AND:

WHERE OrderDate LIKE '2016%' AND OrderDate LIKE '2018%';

But that can't be true for any value. Nothing can start both with 2016 and 2018 at the same time. Maybe you wanted OR instead of AND?

huangapple
  • 本文由 发表于 2023年2月24日 00:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定