Mysql在获取结果中返回值为0
我有这两个表,
--
-- Table structure for table `t1`
--
CREATE TABLE `t1` (
`pid` varchar(20) collate latin1_general_ci NOT NULL,
`pname` varchar(20) collate latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
--
-- Dumping data for table `t1`
--
INSERT INTO `t1` VALUES ('p1', 'pro1');
INSERT INTO `t1` VALUES ('p2', 'pro2');
-- --------------------------------------------------------
--
-- Table structure for table `t2`
--
CREATE TABLE `t2` (
`pid` varchar(20) collate latin1_general_ci NOT NULL,
`year` int(6) NOT NULL,
`price` int(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
--
-- Dumping data for table `t2`
--
INSERT INTO `t2` VALUES ('p1', 2009, 50);
INSERT INTO `t2` VALUES ('p1', 2010, 60);
INSERT INTO `t2` VALUES ('p3', 2007, 200);
INSERT INTO `t2` VALUES ('p4', 2008, 501);
我的查询是,
SELECT *
FROM `t1`
LEFT JOIN `t2` ON t1.pid = t2.pid
获取结果,
pid pname pid year price
p1 pro1 p1 2009 50
p1 pro1 p1 2010 60
p2 pro2 NULL NULL NULL
我的问题是,我想获取价格值为 0 而不是 NULL。我如何编写查询来获取价格值为 0。
提前感谢您的帮助。
I have this two tables,
--
-- Table structure for table `t1`
--
CREATE TABLE `t1` (
`pid` varchar(20) collate latin1_general_ci NOT NULL,
`pname` varchar(20) collate latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
--
-- Dumping data for table `t1`
--
INSERT INTO `t1` VALUES ('p1', 'pro1');
INSERT INTO `t1` VALUES ('p2', 'pro2');
-- --------------------------------------------------------
--
-- Table structure for table `t2`
--
CREATE TABLE `t2` (
`pid` varchar(20) collate latin1_general_ci NOT NULL,
`year` int(6) NOT NULL,
`price` int(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
--
-- Dumping data for table `t2`
--
INSERT INTO `t2` VALUES ('p1', 2009, 50);
INSERT INTO `t2` VALUES ('p1', 2010, 60);
INSERT INTO `t2` VALUES ('p3', 2007, 200);
INSERT INTO `t2` VALUES ('p4', 2008, 501);
my query is,
SELECT *
FROM `t1`
LEFT JOIN `t2` ON t1.pid = t2.pid
Getting the result,
pid pname pid year price
p1 pro1 p1 2009 50
p1 pro1 p1 2010 60
p2 pro2 NULL NULL NULL
My question is, i want to get the price value is 0 instead of NULL. How can i write the query to getting the price value is 0.
Thanks in advance for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要使用
IFNULL
IFNULL(field_to_test, value_to_show) 当field_to_test字段为null时返回value_to_show,当field_to_test不为null时直接返回。
you need to use
IFNULL
IFNULL(field_to_test, value_to_show) will return 'value_to_show' when the field 'field_to_test' is null, when field_to_test is not null, it is returned directly.