我有这样的MySQL查询:(我使用CodeIgniter)
$ report = $ this-> db-> query(“ SELECT c.categoryName, 注1 *。, ((SELECT SUM(noteAmount) 来自……
查询
SELECT noteDate FROM notes GROUP BY noteDate HAVING COUNT(noteDate) > 0
正在选择整个 notes 表格为 note2 (包括cash_out条目)和过滤仅在条目中进行 note1.noteType = 'cash_in' 。这将给两者 cash_in 和 cash_out 作为连接条件的条目仅检查 note1.noteDate = note2.noteDate 总会有一些匹配。
notes
note2
note1.noteType = 'cash_in'
cash_in
cash_out
note1.noteDate = note2.noteDate
可能会添加 where 里面的条件 on 条件,看看你是否想要。
where
on
我相信你错过了一个 where 在你的 JOIN 声明。如果这是在ON元素上或在SELECT本身内,我认为这将解决您的问题。
JOIN
可能是一个错字 cash_in 和 in 。
in
$report = $this->db->query(" SELECT c.categoryName, note1.*, ((SELECT SUM(noteAmount) FROM notes WHERE DATE_FORMAT(noteDate, '%d-%m-%Y') = DATE_FORMAT(note1.noteDate, '%d-%m-%Y') AND noteType = 'in') - (SELECT SUM(noteAmount) FROM notes WHERE DATE_FORMAT(noteDate, '%d-%m-%Y') = DATE_FORMAT(note1.noteDate, '%d-%m-%Y') AND noteType = 'out')) as trxCount FROM notes AS note1 JOIN (SELECT noteDate FROM notes GROUP BY noteDate HAVING COUNT(noteDate) > 0) AS note2 ON note1.noteDate = note2.noteDate JOIN category c ON c.categoryID = note1.categoryID WHERE note1.noteType = 'in' ORDER BY note1.noteDate DESC LIMIT $start, $per_page ")->result();