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

tv6aics1  于 2023-11-21  发布在  SQLite
关注(0)|答案(2)|浏览(201)

我在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
);

字符串
示例数据:
| 亚传媒|MASP|马赫|马杭|东田|ngay|索隆|
| --|--|--|--|--|--|--|
| 1 | 1 |一| 1 | 2000 |2023/11/20 11:43:01| 1 |
| 2 | 2 |一| 1 | 2200 |2023/11/20 11:43:01| 1 |
以下是我的查询:

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


我希望此查询返回tongtien列从2023年11月1日到11月20日的总值。然而,它返回NULL而没有任何错误。您能帮助我确定原因吗?

cwtwac6a

cwtwac6a1#

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

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

wdebmtf2

wdebmtf22#

ngay >= '2023/11/01' AND ngay <= '2023/11/20'相当于ngay >= '2023/11/01 00:00:00' AND ngay <= '2023/11/20 00:00:00';
在比较日期时间时,您需要指定时间:

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

字符串

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


Demo here

相关问题