SQL语句

CREATE TABLE `stock` (
  `outcount` int(11) default NULL,
  `incount` int(11) default NULL,
  `dd` date NOT NULL default '0000-00-00',
  `pid` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`pid`,`dd`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;

 

DROP TABLE IF EXISTS `stock`;
CREATE TABLE `stock` (
  `outcount` int(11) default NULL,
  `incount` int(11) default NULL,
  `dd` date NOT NULL default '0000-00-00',
  `pid` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`pid`,`dd`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;

INSERT INTO `stock` VALUES (0,100,'2008-05-12','A001');
INSERT INTO `stock` VALUES (10,0,'2008-05-13','A001');
INSERT INTO `stock` VALUES (30,0,'2008-05-14','A001');
INSERT INTO `stock` VALUES (0,50,'2008-05-15','A002');
INSERT INTO `stock` VALUES (15,0,'2008-05-16','A002');

 

 

  pid        dd         incount    outcount
A001    2008-05-12    100    0
A001    2008-05-13    0    10
A001    2008-05-14    0    30
A002    2008-05-15    50    0
A002    2008-05-16    0    15


用一句SQL生成如果查询结果.

A001    2008-05-12    100    0    100
A001    2008-05-13    0    10    90
A001    2008-05-14    0    30    60
A002    2008-05-15    50    0    50
A002    2008-05-16    0    15    35

 

 

你可能感兴趣的:(sql)