【问题标题】:Mysql where clause not work in codeigniterMysql where子句在codeigniter中不起作用
【发布时间】:2019-03-14 10:21:32
【问题描述】:

我有这样的 MySQL 查询:(我使用 CodeIgniter)

$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 = 'cash_in')
            -
            (SELECT SUM(noteAmount)
             FROM notes
             WHERE DATE_FORMAT(noteDate, '%d-%m-%Y') = DATE_FORMAT(note1.noteDate, '%d-%m-%Y') AND noteType = 'cash_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 = 'cash_in'
            ORDER BY note1.noteDate DESC
            LIMIT $start, $per_page
          ")->result();

查看 WHERE 子句:WHERE note1.noteType = 'cash_in'

我只想拿现金数据,但为什么我得到所有数据? (包括提现等)。我的表也有提现数据

备注表:

noteID |注意标题 |注意日期 |注意金额 |类别ID |笔记类型

分类表

类别ID |类别名称 |父ID

【问题讨论】:

  • 您遇到什么错误?还请发布注释和类别表中的一些示例数据以及预期的输出。
  • 没有错误。但查询返回所有数据(兑现和兑现)。我只想拿现金
  • 将示例数据添加到您的表中

标签: php mysql sql codeigniter


【解决方案1】:

查询

SELECT noteDate
          FROM notes
          GROUP BY noteDate
          HAVING COUNT(noteDate) > 0

将整个notes 表选择为note2(包括cash_out 条目),并且仅对note1.noteType = 'cash_in' 的条目进行过滤。这将同时提供 cash_incash_out 条目,因为连接条件仅检查 note1.noteDate = note2.noteDate ,这将始终有一些匹配。

可能在on 条件中添加where 条件,看看是否是您想要的。

【讨论】:

    【解决方案2】:

    可能是cash_inin 的拼写错误。

    $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();
    

    【讨论】:

      【解决方案3】:

      我相信您的JOIN 声明中缺少where。如果这发生在 ON 元素或 SELECT 本身内,我相信这会解决您的问题。

      【讨论】:

        猜你喜欢
        • 2016-11-13
        • 2014-01-26
        • 1970-01-01
        • 2016-07-24
        • 2014-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-16
        相关资源
        最近更新 更多