SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE IF NOT EXISTS tablename ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `type` int(11) NOT NULL, `sum` varchar(50) NOT NULL default '0', PRIMARY KEY ( `id` ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; INSERT INTO tablename (`type`, `sum`) VALUES (0, 50), (1, 10), (1, 30), (1, 120), (0, 75), (0, 150); select sum(if(`type` = 0, `sum`, 0)) `income`, sum(if(`type` = 1, `sum`, 0)) `outcome`, sum(if(`type` = 0, `sum`, -`sum`)) `balance` from tablename; select `income`, `outcome`, (`income` - `outcome`) `balance` from ( select sum(if(`type` = 0, `sum`, 0)) `income`, sum(if(`type` = 1, `sum`, 0)) `outcome` from tablename ) `data`;
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear