当使用不相等时,MySQL子查询返回多行

时间:2015-12-08 12:21:00

标签: mysql

Template.gallery.rendered = function(){
     this.$('#grid-container').sortable({
         stop: function(e, ui) {
              el = ui.item.get(0)
              before = ui.item.prev().get(0)
              after = ui.item.next().get(0)
              if(!before) {
                  newRank = Blaze.getData(after).rank - 1
              } else if(!after) {
                  newRank = Blaze.getData(before).rank + 1
              } else {
                  newRank = (Blaze.getData(after).rank + Blaze.getData(before).rank)/2
              }
              Images.update({_id: Blaze.getData(el)._id}, {$set: {rank: newRank}})
        }
    })
}

Template.gallery.helpers({
    'getCurrentCategory': function() {
        return Template.instance().currentcategory.get();
    },
    'images': function (currentcategory) {
        if(currentcategory == 'all' || !currentcategory){
            return Images.find({},{sort: {rank: 1}});
        } 
        return Images.find({category:currentcategory});
    }
});

2 个答案:

答案 0 :(得分:1)

NOT IN不允许空值,因此如果结果具有空值,则不会给出结果。 用这个:

SELECT DISTINCT category_id,name 
FROM ek_category_description 
WHERE category_id NOT EXISTS( SELECT DISTINCT parent_id FROM ek_category) 
AND name LIKE '$searchvalue%'

即使使用空值,NOT EXISTS也会给出结果。 <>是一个单数运算符NOT,因此使用NOT IN效果会更好。

答案 1 :(得分:0)

使用NOT IN

SELECT DISTINCT category_id,name 
FROM ek_category_description 
WHERE category_id NOT IN( SELECT DISTINCT parent_id FROM ek_category) 
AND name LIKE '$searchvalue%'