如果条件在查询中

时间:2013-09-03 22:21:02

标签: mysql sql

我正在尝试使用if语句,就像标题是这样,排除那些文件名,否则filenames = bla bla。但显然没有工作,有什么想法?

SELECT
    SUM(streamlength)
FROM
    files,
    filemetadata
where
    files.id = filemetadata.id 
    AND title like 'abc'
    and (Time > '2013-01-01' and Time < '2013-08-06' )
    and 
    if((title like 'abc' AND Time > '2013-02-22'),
    (filename NOT like '%20121129%'
    AND filename NOT like '%20121204%'
    AND filename NOT like '%20121214%'
    AND  filename NOT like '%20121219%'
    AND filename NOT like '%20130109%'
    AND  filename NOT like '%20130114%'),filename like 
                   '%1111111%')

1 个答案:

答案 0 :(得分:3)

如果我正确地读取了SQL的意图:

SELECT
SUM(streamlength)
FROM
files,
filemetadata
where
files.id = filemetadata.id 
AND title like 'abc'
and (Time > '2013-01-01' and Time < '2013-08-06' )
and 
(
  ( 
    (title like 'DE-PIAE78' AND Time > '2013-02-22')
    AND
    (filename NOT like '%20121129%'
     AND filename NOT like '%20121204%'
     AND filename NOT like '%20121214%'
     AND  filename NOT like '%20121219%'
     AND filename NOT like '%20130109%'
     AND  filename NOT like '%20130114%')
  )
  OR
  (
    NOT ( (title like 'DE-PIAE78' AND Time > '2013-02-22') )
    AND ( filename like '%1111111%')
  )
)

根据(title like 'DE-PIAE78' AND Time > '2013-02-22')是真还是假来匹配不同的文件名。

某些SQL变体支持IF语句,但不是全部都支持。