如何在mysql中查询字段不为空或null的所有记录?

时间:2013-10-12 08:23:49

标签: mysql

如何在mysql中查询字段不为空或空的所有记录?

如下文所述,有些products_name为空,有些为null,如何获取其他有值记录? enter image description here

-- ----------------------------
-- Table structure for `a`
-- ----------------------------
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
  `products_id` int(11) NOT NULL,
  `products_name` varchar(255) default NULL,
  PRIMARY KEY  (`products_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of a
-- ----------------------------
INSERT INTO `a` VALUES ('1', 'hello');
INSERT INTO `a` VALUES ('2', '222');
INSERT INTO `a` VALUES ('3', null);
INSERT INTO `a` VALUES ('4', '');
INSERT INTO `a` VALUES ('5', '5555');

2 个答案:

答案 0 :(得分:3)

你可以这样做:

SELECT * FROM `a` WHERE `products_name` IS NOT NULL AND `products_name` != '';

答案 1 :(得分:3)

你可以简洁地表达出来:

SELECT * FROM a
WHERE ifnull(products_name, '') != ''