between和where?

lvmkulzt  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(136)

我试着同时使用where和between

SELECT 
SUM ((`gen-acute-sales-after-returns`) + SUM(`gen-chronic-sales-after- returns`)) 
  AS `totalGenericSales` ,
(SUM (`eth-acute-sales-after-returns`) + SUM(`eth-chronic-sales-after-returns`)+SUM(`others-sales-after-returns`))
AS `totalEthicalSales` 
FROM `sales` WHERE `date-s` BETWEEN '2018-09-27' AND '2018-11-27' 
WHERE `sales-store-id` = '4'

问题是如果我用where sales-store-id
哪里 date-s 在“2018-09-27”和“2018-11-27”之间
它工作得很好,但当我同时使用这两种语言时,它会产生语法错误
未处理的拒绝错误:er\u parse\u error:您的sql语法有错误;请查看与mysql服务器版本对应的手册,以了解在“where”附近使用的正确语法 sales-store-id = '4''
怎么回事?有人能解释一下吗?

vnzz0bqm

vnzz0bqm1#

你没有具体说明 Where 两次,如果你想同时有多个条件。你用 And / Or 而不是逻辑运算符。

SELECT 
  SUM ((`gen-acute-sales-after-returns`) + SUM(`gen-chronic-sales-after- returns`)) 
  AS `totalGenericSales` ,
  (SUM (`eth-acute-sales-after-returns`) + SUM(`eth-chronic-sales-after-returns`) + SUM(`others-sales-after-returns`))
  AS `totalEthicalSales` 
FROM `sales` 
WHERE `date-s` BETWEEN '2018-09-27' AND '2018-11-27' 
AND `sales-store-id` = '4' /* use AND here instead of WHERE */
oyjwcjzk

oyjwcjzk2#

如果您希望销售商店id为4:

SELECT SUM((`gen-acute-sales-after-returns`) + SUM(`gen-chronic-sales-after- returns`))
    AS `totalGenericSales`,
       (SUM(`eth-acute-sales-after-returns`) + SUM(`eth-chronic-sales-after-returns`) +
        SUM(`others-sales-after-returns`))
    AS `totalEthicalSales`
FROM `sales`
WHERE `date-s` BETWEEN '2018-09-27' AND '2018-11-27'
    AND `sales-store-id` = '4'
mklgxw1f

mklgxw1f3#

只需使用和继续查询。

SELECT 
SUM ((`gen-acute-sales-after-returns`) + SUM(`gen-chronic-sales-after- returns`)) 
AS `totalGenericSales` ,
(SUM (`eth-acute-sales-after-returns`) + SUM(`eth-chronic-sales-after- 
returns`)+SUM(`others-sales-after-returns`))
AS `totalEthicalSales` 
FROM `sales` WHERE `date-s` BETWEEN '2018-09-27' AND '2018-11-27' 
AND `sales-store-id` = 4

相关问题