[BUG] AND clauses ignored
(1) By Hans B (HansBull) on 2026-05-31 11:26:37 [source]
sqlite 3.45.1 on Ubuntu 24.04 I have to admit that the CAST part is not particularly pretty, but the columns are a mixed data type, with some rows having letters and not numbers.
CREATE TABLE "tc" ( "n" TEXT NOT NULL, "an" INTEGER, "ltr" TEXT, "cat" TEXT, "ffrom" TEXT, "tto" TEXT, "note" TEXT );
INSERT INTO "tc" ("n", "an", "ltr", "cat", "ffrom", "tto", "note") VALUES ('2349', '1827', 'III', NULL, '2211', '2325', NULL);
INSERT INTO "tc" ("n", "an", "ltr", "cat", "ffrom", "tto", "note") VALUES ('341', '1836', 'II', NULL, '2211', '2325', NULL);
These queries return both rows, but should not:
SELECT * FROM "tc" WHERE "an"=1822 AND "ltr"='II' AND (2211 BETWEEN CAST(ffrom AS INT) AND CAST(tto AS INT)) OR (CAST(ffrom AS INT) = 2211);
SELECT * FROM "tc" WHERE "ltr"='II' AND (2211 BETWEEN CAST(ffrom AS INT) AND CAST(tto AS INT)) OR (CAST(ffrom AS INT) = 2211);
SELECT * FROM "tc" WHERE "an"=1822 AND (2211 BETWEEN CAST(ffrom AS INT) AND CAST(tto AS INT)) OR (CAST(ffrom AS INT) = 2211);
(2) By jchd (jchd18) on 2026-05-31 12:28:14 in reply to 1 [link] [source]
As in many languages (e.g. C), AND has precedence over OR. Hence imagine a parenthesis around the list of AND conditions, so that your queries' conditions look like A or B.The OR condition being TRUE short-circuits the block and the results are what they should be.
(3) By Hans B (HansBull) on 2026-05-31 17:17:05 in reply to 2 [link] [source]
You are absolutely right, I had a parenthesis mismatch and the query works now fine. Sorry for the noise.