1

how can i query all records in mysql where field is not empty or null?

such as bellow, some products_name is empty ,and some is null , how to get other has values records? 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 Answers 2

3

You could just do:

SELECT * FROM `a` WHERE `products_name` IS NOT NULL AND `products_name` != '';
Sign up to request clarification or add additional context in comments.

4 Comments

Try to use IFNULL
Why would you need IFNULL? He wants to get the records where the name is NOT NULL and not empty isnt it? In case he wants all records but wants to do something else with the NULL fields he could use IFNULL. Correct me if i'm wrong.
how can i query all records in mysql where field is not empty or null?
Just change the AND Constraint in the query i posted above to an OR constraint.
3

You can express it succinctly as:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.