我該如何修復并可能簡化我猜這個 SQL 查詢?
SELECT * FROM table WHERE
M IN (NULL,1) AND T IN (NULL,1) AND J IN (NULL,1) AND B IN (NULL,1)
AND (ISNULL(M,0) ISNULL(T,0) ISNULL(J,0) ISNULL(B,0))<4
ORDER BY (ISNULL(M,0) ISNULL(T,0) ISNULL(J,0) ISNULL(B,0)) DESC
table包含 4 列(M,T,J,B),只有 3 個可能的值NULL, 0, 1。
- 僅獲取包含
NULL或的條目的第一行過濾器1。 - 第二行做加法只得到
M J T B和過濾器的總數if <4。 - 三線
ORDER BY相同的總,M J T B。
錯誤 #1582 - Incorrect parameter count in the call to native function 'ISNULL'
MySQL 相當于ISNULL是IFNULL...在這里回答
uj5u.com熱心網友回復:
當然未經測驗,但以下可能是您所追求的
select * from (
select *, coalesce(M,0) M2, coalesce(T,0) T2, coalesce(J,0) J2, coalesce(B,0) B2
from table
where
(M is null or M=1) and
(T is null or T=1) and
(J is null or J=1) and
(B is null or B=1)
)x
where M2 T2 J2 B2 < 4
order by M2 T2 J2 B2 desc
uj5u.com熱心網友回復:
空值在 SQL 中表現得很奇怪。 任何帶有 null 的比較測驗都將導致 FALSE,因此如果您在 where 子句中使用短語 Null=Null,則不會檢索任何內容,因為結果始終為 false。我會改寫你的第二行以使用 IsNull 函式,
IsNull(M,1)*IsNull(T,1)*IsNull(J,1)*IsNull(B,1)>0
uj5u.com熱心網友回復:
Select
t.*,
coalesce(M,0) coalesce(T,0) coalesce(J,0) coalesce(B,0) as calc
from table t
where
coalesce(M,0) in (0,1) and
coalesce(T,0) in (0,1) and
coalesce(J,0) in (0,1) and
coalesce(B,0) in (0,1) and
calc < 4
order by calc desc
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351132.html
