3

I have a MySQL query in CodeIgniter like this:

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

See on WHERE Clause:

WHERE note1.noteType = 'cash_in'

I only want to take the cash data, but why do I get all the data (including cash out and others)?

My table also has cash out data.

notes table:

| noteID | noteTitle | noteDate | noteAmount | categoryID | noteType |

category table:

| categoryID | categoryName | parentID |

3
  • 1
    What error are you getting? Also please post some sample data from the notes and category table as well as an expected output please. Commented Mar 14, 2019 at 10:30
  • No error. But the query return all data (cash out and cash in). I only want to get cash in Commented Mar 14, 2019 at 10:36
  • Add sample data to your tables Commented Mar 14, 2019 at 10:47

3 Answers 3

1

The query

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

is selecting the entire notes tables as note2 (including cash_out entries) and filtering is being done only on entries where note1.noteType = 'cash_in'. This will give both cash_in and cash_out entries as the join condition only checks for note1.noteDate = note2.noteDate which will always have some match.

Probably add where condition inside the on condition and see if that is what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

Possibly a typo with cash_in and 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();

Comments

0

I believe you are missing a where on your JOIN statement. If this went on the ON element or within the SELECT itself I believe that would fix your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.