我有一个表,我存储了一些资源的ID。我想为KeyValue词典制作SQL返回值,而无需加入。
使用联接可能是可能的。还有其他方法可以做到吗,因为我在密钥/值数组中只有几个元素。
编程语言并不重要。我只是在寻找能够做到的SQL。postgresql或mysql。
表:
|objectId|attribute1|attribute2|
|45 | x | y |
|67 | x1 | y2 |
...
我有一个dict(对象/dict/键值数组):
{ 45: "My title", 67: "Other title" }
和SQL结果应包含标题而不是objectid的值。
select objectId, attribute1
from table
将返回:
My title | x
Other title | x1
您是否正在寻找case
表达式?
select (case when objectId = 45 then 'My title'
when objectId = 67 then 'Other title'
end),
attribute1
from t;