此查询仍返回空标题字段?

时间:2011-10-25 19:38:28

标签: mysql sql select

此查询完美无缺,它仍会返回空白标题字段:

SELECT
    DISTINCT( TRIM( LEFT(title, 
    IF( LOCATE('10 Pack', title), LOCATE('10 Pack', title) - 1, 
    IF( LOCATE('100 Classic', title), LOCATE('100 Classic', title) - 1, 
    IF( LOCATE('100 Favourite', title), LOCATE('100 Favourite', title) - 1, 
    IF( LOCATE('DVD', title), LOCATE('DVD', title) - 1,
    IF( LOCATE('Bluray', title), LOCATE('Bluray', title) - 1,
    IF( LOCATE('Series', title), LOCATE('Series', title) - 1,
    IF( LOCATE('20 ', title), LOCATE('20 ', title) - 1,
999 )))))))))), 
    main_category, 
    genre, 
    actors
FROM 
    PRprodINFO 
WHERE
    ((main_category = 'Films') 
    AND (length(title) > 2)) 
GROUP BY title

也尝试过:

SELECT 
    DISTINCT( TRIM( LEFT(title, 
    IF( LOCATE('10 Pack', title), LOCATE('10 Pack', title) - 1, 
    IF( LOCATE('100 Classic', title), LOCATE('100 Classic', title) - 1, 
    IF( LOCATE('100 Favourite', title), LOCATE('100 Favourite', title) - 1, 
    IF( LOCATE('DVD', title), LOCATE('DVD', title) - 1,
    IF( LOCATE('Bluray', title), LOCATE('Bluray', title) - 1,
    IF( LOCATE('Series', title), LOCATE('Series', title) - 1,
    IF( LOCATE('20 ', title), LOCATE('20 ', title) - 1,
999 )))))))))),
    main_category, 
    genre, 
    actors
FROM
    PRprodINFO
where 
    ((main_category = 'Films')
    AND (title <> ''))
GROUP BY title

但仍返回空白的标题字段行...任何想法为什么?

由于

的Darren

3 个答案:

答案 0 :(得分:0)

如果字段为null,则LOCATE(astr, field)将返回null。 您需要确保title不为空 要么title替换为COALESCE(title),请执行以下操作:

SELECT DISTINCT( TRIM( LEFT(i.title, 
  IF( LOCATE('10 Pack', i.title), LOCATE('10 Pack', i.title) - 1, 
  IF( LOCATE('100 Classic', i.title), LOCATE('100 Classic', i.title) - 1, 
  IF( LOCATE('100 Favourite', i.title), LOCATE('100 Favourite', i.title) - 1, 
  IF( LOCATE('DVD', i.title), LOCATE('DVD', i.title) - 1,
  IF( LOCATE('Bluray', i.title), LOCATE('Bluray', i.title) - 1,
  IF( LOCATE('Series', i.title), LOCATE('Series', i.title) - 1,
  IF( LOCATE('20 ', i.title), LOCATE('20 ', i.title) - 1,999 ))))))))))
  , i.main_category, i.genre, i.actors 
FROM (SELECT main_category, genre, actors, COALESCE(title,'') as title 
      FROM PRprodINFO) i 
WHERE ((i.main_category = 'Films') AND (length(i.title) > 2)) 
GROUP BY i.title

答案 1 :(得分:0)

我会将以下内容添加到WHERE子句中......

AND Length( TRIM( NVL(title, '' ))) > 2

代替你的

AND (title <> '' )

如果您有任何NULL值,那么它们会将其抛弃。此外,如果标题的值为“”,那么也会产生误报。因此,通过执行NVL()(如果为空值,使用''),然后修剪它,然后检查长度......您应该完全满足您的需求。

答案 2 :(得分:0)

尝试修改大量IF块,以便在没有满足任何条件时返回title。结果是什么?

SELECT 
    DISTINCT( TRIM( LEFT(title, 
    IF( LOCATE('10 Pack', title), LOCATE('10 Pack', title) - 1, 
    IF( LOCATE('100 Classic', title), LOCATE('100 Classic', title) - 1, 
    IF( LOCATE('100 Favourite', title), LOCATE('100 Favourite', title) - 1, 
    IF( LOCATE('DVD', title), LOCATE('DVD', title) - 1,
    IF( LOCATE('Bluray', title), LOCATE('Bluray', title) - 1,
    IF( LOCATE('Series', title), LOCATE('Series', title) - 1,
    IF( LOCATE('20 ', title), LOCATE('20 ', title) - 1,
999 )))))), title )))),
    main_category, 
    genre, 
    actors
FROM
    PRprodINFO
where 
    ((main_category = 'Films')
    AND (title <> ''))
GROUP BY title