SQL 查询未在 SQLite 中返回预期结果

问题描述 投票:0回答:1

我在 SQLite 中遇到 SQL 查询问题,我不明白为什么它没有返回预期结果。下面是表格和示例数据:

CREATE TABLE Giohang (
    magh INTEGER PRIMARY KEY AUTOINCREMENT,
    masp INTEGER REFERENCES Sanpham (masp),
    makh TEXT REFERENCES Khachhang (makh),
    mahang INTEGER REFERENCES Hangsp (mahang),
    tongtien INTEGER NOT NULL,
    ngay DATE,
    soluong INTEGER NOT NULL
);

样本数据:

https://i.stack.imgur.com/zC69p.png

这是我的查询:

SELECT SUM(tongtien) AS doanhthu 
FROM Giohang 
WHERE ngay >= '2023/11/01' AND ngay <= '2023/11/20';

我希望此查询返回 2023 年 11 月 1 日至 11 月 20 日期间

tongtien
列的总值。但是,它返回
NULL
,没有任何错误。你能帮我找出原因吗?

https://i.stack.imgur.com/05GyO.png

sqlite
1个回答
0
投票

此搜索获取给定年份值的两个月之间的所有数据

 ElseIf gvSearchType = "MoRangeYr" Then
                cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth >= $gvFromMonth AND txSearchMonth <= $gvToMonth AND txYear = $gvYear "
                'cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth BETWEEN $gvFromMonth AND $gvToMonth AND txYear = $gvYear "
                cmd.Parameters.AddWithValue("$gvFromMonth", gvFromMonth)
                cmd.Parameters.AddWithValue("$gvToMonth", gvToMonth)
                cmd.Parameters.AddWithValue("$gvYear", gvYear)

然后使用此代码将数据显示在 DataGridView 中

                Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
                'dgvTX.DataSource = rdr
                'Statement Above is when DB is bound to dgvTX
                '============================================

                While rdr.Read()
                    intID = CInt((rdr("TID")))
                    strDate = rdr("txSortDate").ToString 'txSearchMonth '
                    strTxType = rdr("txType").ToString
                    strAmt = CDec(rdr("txAmount"))
                    strCKNum = rdr("txCKNum").ToString
                    strDesc = rdr("txDesc").ToString
                    strBal = CDec(rdr("txBalance"))
                    dgvTX.Columns(3).DefaultCellStyle.Format = "N"
                    dgvTX.Columns(6).DefaultCellStyle.Format = "N"
                    'dgvTX.Columns(6).DefaultCellStyle.Format = "C"'Adds the $ sign and commas
                    dgvTX.Rows.Add(intID, strDate, strTxType, strAmt, strCKNum, strDesc, strBal, emptyStr)

                    rowCount = rowCount + 1

                End While

                dgvTX.Columns(3).DefaultCellStyle.Format = "N"
                dgvTX.Columns(6).DefaultCellStyle.Format = "N"
                dgvTX.Sort(dgvTX.Columns(0), ListSortDirection.Descending)
            End Using
© www.soinside.com 2019 - 2024. All rights reserved.