我有一张表,其表结构如下
POST /logstash-2016.05.19/_search
{
"query": {
"match": { "serviceName": "systemA" }
}
}
我将从前端获得两个不同的pincodes。比方说695582和685608.我需要通过单个查询得到这些密码的service_types,结果应该是单行。我尝试过UNION查询,但结果分为两行。
id service_type pincode
---------------------------------
1 B 695582
2 D 676102
3 P 685584
4 B 685608
以上查询的结果如下
select 'buyer' name, service_type from tm_location_carrier_lnk where pincode=695582
UNION
select 'seller' name, service_type from tm_location_carrier_lnk where pincode=685608
如何在单行中获得结果如下
name service_type
------------------
buyer B
seller B
答案 0 :(得分:3)
为什么不这样做
SELECT (select service_type from tm_location_carrier_lnk where pincode=695582 LIMIT 1) as Buyer, (select service_type from tm_location_carrier_lnk where pincode=685608 LIMIT 1) as Seller;
您可以在这里找到一个有效的例子:http://sqlfiddle.com/#!9/e01426/1
编辑:添加LIMIT 1以避免重复的密码。
答案 1 :(得分:1)
一种解决方案,假设有独特的密码并且特定于您的请求:
Select
(select max(service_type)
from tm_location_carrier_lnk
where pincode=695582) as Buyer
, (select max(service_type)
from tm_location_carrier_lnk
where pincode=685608) as Seller;