我有一个表结构(我没有设计也不能更改),它使用varchar
字段来存储关于实体的属性。我想编写一个SQL查询来特别搜索两个属性,并将多个结果行合并为单个行。为了说明这一点,我的表与此类似:Oracle group by SQL查询通过匹配varchar字段
company
=============
| id | name |
-------------
| 1 | co1 |
| 2 | co2 |
| 3 | co3 |
=============
agent
====================================
| id | name | company_id | type |
------------------------------------
| 1 | Tom | 1 | 'type1' |
| 2 | Bob | 1 | 'type2' |
| 3 | Bill | 2 | 'type1' |
| 4 | Jack | 2 | 'type2' |
| 5 | John | 3 | 'type1' |
| 6 | Joe | 3 | 'type2' |
====================================
type1
和type2
是硬编码到软件作为有效值(再次,我没有写它),所以对于这些值的搜索应该是成功的(允许为空)。所以,我必须将我的搜索建立在这些值的基础上。
作为一个新手,我可以写这样的SQL:
select c.name, a.name, a.type
from company c
inner join agent a on c.id = a.company_id
,并分类整理这些结果在我的软件(Java程序):
===========================
| c.name | a.name | type |
---------------------------
| co1 | Tom | type1 |
| co1 | Bob | type2 |
| co2 | Bill | type1 |
| co2 | Jack | type2 |
| co3 | John | type1 |
| co3 | Joe | type2 |
===========================
但是,我希望会有一个将行组合成更有效率的方式:
-- my failed attempt at writing this query
select c.name, a.name as type_1_agent, a.name as type_2_agent
from company c
inner join agent a on c.id = a.company_id
group by c.id -- ?
where -- ?
results:
======================================
| name | type_1_agent | type_2_agent |
--------------------------------------
| co1 | Tom | Bob |
| co2 | Bill | Jack |
| co3 | John | Joe |
======================================
这可能吗?