SELECT * FROM company_disposition WHERE company_id = $1 AND campaign_uuid = 'null' OR campaign_uuid != $2
这是我的postgres请求..我不想获得所有值which company_id = $1 AND campaign_uuid = 'null' OR campaign_uuid != $2
..如何制作它?我看到的请求无效//
{
"company_id": "be1b5ee1-51e7-422b-b469-592dbd62bec9",
"disposition": "string",
"disposition_uuid": "8f66c72a-9a68-440a-bd7f-3a0f49ef4973",
"disposition_order": 2323,
"campaign_uuid": "fd80a1fa-947d-4b09-a755-cee5951b6c85"
},
{
"company_id": "be1b5ee1-51e7-422b-b469-592dbd62bec9",
"disposition": "strinsdsdg",
"disposition_uuid": "4c8e9601-1894-4e69-b22c-f2b83d9d80bb",
"disposition_order": 2323,
"campaign_uuid": null
}
我想得到这个:
{
"company_id": "be1b5ee1-51e7-422b-b469-592dbd62bec9",
"disposition": "strinsdsdg",
"disposition_uuid": "4c8e9601-1894-4e69-b22c-f2b83d9d80bb",
"disposition_order": 2323,
"campaign_uuid": null
}
"campaign_uuid"
- 可以是值或null
答案 0 :(得分:1)
您的查询有两个问题(如果我正确阅读):
使用=
运算符与NULL进行比较。
SQL对比较运算符使用三值逻辑,因此将NULL(= unknown)与任何事物进行比较既不是TRUE
也不是FALSE
,而是NULL。
这意味着条件something = NULL
永远不会TRUE
(它始终为NULL)。
您必须使用IS NULL
运算符来测试值是否为NULL。
您忘记AND
的优先级高于OR
,因此您必须使用括号。
我认为你的意思是:
SELECT * FROM company_disposition
WHERE company_id = $1
AND (campaign_uuid IS NULL OR campaign_uuid <> $2);
如果您从未提供$ 2的NULL值,则更简单的方法是
SELECT * FROM company_disposition
WHERE company_id = $1
AND campaign_uuid IS DISTINCT FROM $2;
答案 1 :(得分:0)
SELECT * FROM company_disposition WHERE company_id = $1 AND campaign_uuid is null OR campaign_uuid <> $2 AND company_id = $1
答案 2 :(得分:0)
尝试:
SELECT * FROM company_disposition WHERE company_id = $1 AND (campaign_uuid is null OR campaign_uuid <> $2)
此查询可能会返回多行,所有行都包含company_id = $ 1和campaign_uuid!= $ 2,(条件campaign_uuid!= $ 2将包含campaign_uuid = null的行),如果您只想获取一行每个company_id,你需要使用&#34; DISTINCT&#34;
示例:
SELECT DISTINCT ON (company_id)
company_id,
disposition,
disposition_uuid,
disposition_order,
campaign_uuid
FROM
company_disposition
WHERE
company_id = $1 AND
(campaign_uuid is null OR campaign_uuid <> $2)
ORDER BY company_id