Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql IF-ELSE语句语法错误_Mysql - Fatal编程技术网

mysql IF-ELSE语句语法错误

mysql IF-ELSE语句语法错误,mysql,Mysql,我有以下疑问: SELECT * FROM `hssData` WHERE `group` = 'LGA_737_CA' AND ( `pos` = 'CA' OR `pos` = 'RC' ) AND ( `depTimeInt` >= 0 AND `depTimeInt` <= 1440 ) AND ( `arrTimeInt` >= 1

我有以下疑问:

    SELECT 
     * 
FROM 
    `hssData` 
WHERE 
    `group` = 'LGA_737_CA' 
    AND (
        `pos` = 'CA' OR `pos` = 'RC'
    ) 
    AND (
        `depTimeInt` >= 0 AND `depTimeInt` <= 1440
    ) 
    AND (
        `arrTimeInt` >= 1050 OR `arrTimeInt` <= 45
    ) 
    AND `days` >= 1 
    AND `days` <= 2 
ORDER BY 
    `depTime` ASC 
LIMIT 
    0, 100
选择
* 
从…起
`hssData`
哪里
`组“='LGA\U 737\U CA'
及(
`pos`='CA'或'pos`='RC'
) 
及(
`depTimeInt`>=0和'depTimeInt`=1050或'arrTimeInt`=1
而“days”您正试图在非程序上下文中创建一个

在SELECT(或UPDATE或DELETE)语句中,您需要使用(或CASE)。因此:

选择
* 
从…起
`hssData`
哪里
`组“='LGA\U 737\U CA'
及(
`pos`='CA'
或'pos`='RC'
) 
和(//本子句中的括号是多余的
`depTimeInt`>=0
和'depTimeInt`=1050
或'arrTimeInt`=1

和'days'请添加错误消息“这会引发语法错误”什么错误?
days>=1和days@Martin True,然后代码本身实际上执行相同的小于或等于比较两次。您对SQL语法的理解不足。
if
和friends不能仅嵌入任何其他表达式的任何部分。
if THING then(这个和那个)else(无论什么)
在条件中表示为
(THING and(THIS and THAT))或(not THING and which)
。或者,您可以使用
case
,但我认为这看起来很糟糕:
case when THING然后case when THIS and THAT 1 else 0 end else case when THIS 1 else 0 end else when THIS 1 else 0 end
`days` >= 1 AND `days` <= 1
`days` >= 1 AND `days` <= 2
       SELECT 
    * 
FROM 
    `hssData` 
WHERE 
    `group` = 'LGA_737_CA' 
    AND (
        `pos` = 'CA' OR `pos` = 'RC'
    ) 
    AND (
        `depTimeInt` >= 0 AND `depTimeInt` <= 1440
    ) 
    AND (
        `arrTimeInt` >= 1050 OR `arrTimeInt` <= 45
    ) 
    AND (
        IF (`arrTimeInt` < 45) THEN (
            `days` <= 1 AND `days` <= 3
        ) ELSE (
            `days` <= 1 AND `days` <= 2
        )
    ) 
ORDER BY 
    `depTime` ASC 
LIMIT 
    0, 100
 SELECT 
   * 
 FROM 
    `hssData` 
 WHERE 
  `group` = 'LGA_737_CA' 
  AND (
    `pos` = 'CA' 
    OR `pos` = 'RC'
  ) 
  AND (   // brackets in this clause are redundant
    `depTimeInt` >= 0 
    AND `depTimeInt` <= 1440
  ) 
  AND (
    `arrTimeInt` >= 1050 
    OR `arrTimeInt` <= 45
  ) 
  AND `days` >= 1 
  AND `days` <= IF(`arrTimeInt` <= 45, 1, 2)
ORDER BY 
  `depTime` ASC 
LIMIT 
  0, 100