不一致的MySQL查詢結果
<p>我有一個名為"transaction"的表,保存了在某個支付系統上進行的交易。
我想要獲得2023年7月的每日報告(例如),並運行了以下查詢:</p>
<pre class="brush:sql;toolbar:false;">SELECT DAY(CREATED), count(*) FROM `transaction`
WHERE STATUS = 'approved' AND MONTH(CREATED) = 07 AND YEAR(CREATED) = 2023
GROUP BY DAY(CREATED)
</pre>
<p>結果跳過了一些天,例如對於一個月的第7天,我什麼都沒有得到,但是當我將查詢改為:</p>
<pre class="brush:sql;toolbar:false;">SELECT DAY(CREATED), count(*) FROM `transaction`
WHERE STATUS = 'approved' AND MONTH(CREATED) = 07 AND YEAR(CREATED) = 2023 AND DAY(CREATED) = 7
GROUP BY DAY(CREATED);
</pre>
<p>(基本上在查詢中加入了<code>DAY(CREATED) = 7</code>)</p>
<p>它回傳了第7天的數數。</p>
<p>為什麼會不一致?
在第一個查詢中我該如何解決這個問題? </p>
<p>提前謝謝 :)</p>
<p>編輯:
表結構與資料範例:</p>
<pre class="brush:sql;toolbar:false;">CREATE TABLE `transaction` (
`ID` int NOT NULL,
`PLATFORM_ID` int NOT NULL,
`ENTITY_ID` int NOT NULL,
`RELATED_TRANSACTION_ID` int NOT NULL DEFAULT '0',
`ORIGIN_ID` varchar(120) NOT NULL,
`BANK` varchar(64) DEFAULT NULL,
`AMOUNT` double NOT NULL,
`CURRENCY` varchar(3) NOT NULL,
`TYPE` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`CREATED` datetime NOT NULL,
`CARD_NO` varchar(20) DEFAULT NULL,
`CARD_BRAND` varchar(45) DEFAULT NULL,
`CARD_EXPIRE_YEAR` int DEFAULT NULL,
`CARD_EXPIRE_MONTH` varchar(45) DEFAULT NULL,
`HOLDER_NAME` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`EMAIL` varchar(256) DEFAULT NULL,
`STATUS` enum('approved','declined','filtered','pending') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`IS_3D` tinyint(1) DEFAULT NULL,
`CREATED_AT` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`AMOUNTEUR` double DEFAULT NULL,
`IS_FRAUD` tinyint(1) NOT NULL DEFAULT '0',
`FRAUD_DATE` date DEFAULT NULL,
`KYC_DATE` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
</pre>
<p>範例插入:</p>
<pre class="brush:sql;toolbar:false;">INSERT INTO `transaction` (`ID`, `PLATFORM_ID`, `ENTITY_ID`, `RELATED_TRANSACTION_ID`, `ORIGIN_ID`, `BANK`, `AMOUNT``` , `CURRENCY`, `TYPE`, `CREATED`, `CARD_NO`, `CARD_BRAND`, `CARD_EXPIRE_YEAR`, `CARD_EXPIRE_MONTH`, `HOLDER_NAME`, `EMA, `STATUUS, `IS AMOUNTEUR`, `IS_FRAUD`, `FRAUD_DATE`, `KYC_DATE`) VALUES
(1, 1, 87, 0, '219114359', 'SOME BANK', 150, 'USD', 'DB', '2022-02-01 00:00:17', '000000XXXXXX0000', 'MasterCard', 2025 , '06', 'NAME FAMILY', 'aaaaaaaaa@gmail.com', 'approved', 0, '2022-08-25 13:12:58', 150.7605, 0, NULL, NULL);
</pre>
<p>我省略了索引定義,因為我認為在這種情況下它不相關。 </p>