假設我的桌子上有這些資料:
[
{
name: "test1",
address: "hello",
value: "hi1"
},
{
name: "test2",
address: "hello2",
value: "hi2"
},
{
name: "test3",
address: "hello2",
value: "hi3"
},
{
name: "test4",
address: "hello",
value: "hi4"
},
]
我有一個wherein功能,我需要選擇具有名稱(test1AND 地址hello)和(名稱test2地址hello2)的專案。
所以我為實作這一目標所做的就是通過 concat。
select concat(name, '-', address) as a, name, address from tableName
where a in ("test1-hello", "test2-hello2")
這在 mysql 中可以正常作業,但在 postgress 中你不能在 where 上使用別名,所以我嘗試了:
select concat(name, '-', address) as a, name, address from tableName
where concat(name, '-', address) in ("test1-hello", "test2-hello2")
上面的代碼給了我一個語法錯誤。任何想法如何實作這一目標?
更新:
根據@jarlh 的評論,我找到了我的問題的答案,我不需要使用 concat 并且只需執行以下操作:
where (name, address) in (('test1', 'hello'), ('test2', 'hello2'))
反而。但是,我將保留這個問題,因為可能有人愿意回答如何在 where 子句上使用 concat。
uj5u.com熱心網友回復:
"test1-hello"被讀作識別符號,即這里作為列名。改用單引號來撰寫字串文字。
但是,我會這樣做:
where (name, address) in (('test1', 'hello'), ('test2', 'hello2'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/437064.html
標籤:sql PostgreSQL
上一篇:從一行創建2個逗號分隔的列
