2013-10-12 53 views
1

如何查詢mysql中的所有記錄字段不爲空或爲空?如何查詢mysql中的所有記錄字段不爲空或爲空?

如bellow,有些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'); 

回答

3

你可能只是做:

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

嘗試使用**'IFNULL' ** – Luv

+0

爲什麼你需要IFNULL ?他想要得到名稱不是空的記錄,而不是空的不是嗎?如果他想要所有的記錄,但是想用NULL字段做別的事情,他可以使用IFNULL。如我錯了請糾正我。 –

+0

***我怎麼能查詢所有記錄在mysql中的字段不爲空或空?*** – Luv

3

你可以簡潔地表達它:

SELECT * FROM a 
WHERE ifnull(products_name, '') != '' 
相關問題