我有以下数据库表: COMMENT 和 TAG 和 COMMENT_TAG 。它们之间有许多关系,实际上单个注释可以具有多个标签,而单个标签可以用于多个注释。让我们想象以下内容:
COMMENT
---------------
ID TEXT
1 comment1
2 comment2
3 comment3
4 comment4
TAG
---------------
ID NAME
1 bb
2 cc
3 ca
4 aa
5 ac
6 bd
COMMENT_TAG
---------------
ID_COMMENT ID_TAG
1 1
1 2
1 3
1 4
2 1
2 3
3 3
3 4
3 5
3 6
4 1
4 2
4 3
4 4
4 5
4 6
因此,在FE端显示了以下数据
Comment Tags
==================================
comment1 bb, cc, ca, aa
comment2 bb, ca
comment3 ca, aa, ac, bd
comment4 bb, cc ,ca, aa, ac, bd
我想通过以下方式进行排序:
1 按字母顺序对每个评论的标签进行排序:
Comment Tags
==================================
comment1 aa, bb, ca, cc
comment2 bb, ca
comment3 aa, ac, bd, ca
comment4 aa, ac, bb, bd, ca, cc
2 :按标签的字母顺序对评论进行排序:
Comment Tags
==================================
comment4 aa, ac, bb, bd, ca, cc
comment3 aa, ac, bd, ca
comment1 aa, bb, ca, cc
comment2 bb, ca
您能否建议使用SQL如何实现它,或者建议针对这种用例的其他替代排序方式?
答案 0 :(得分:3)
加入表并按评论分组。
然后针对Oracle 11g +使用类似LISTAGG()
的函数来连接每个注释的所有标签:
<mat-form-field class="example-full-width">
<mat-label>¿Que Servicio Profesional Buscas?</mat-label>
<input type="text" matInput formControlName="oficio" (keypress)="filterList($event)"
[matAutocomplete]="auto" [(ngModel)]="value">
<button mat-button *ngIf="value" matSuffix mat-icon-button aria-label="Clear"
(click)="value=''">
<mat-icon>close</mat-icon>
</button>
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let item of goalList" [value]="item.data.name">
{{item.data.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
为每种情况添加select c.text, listagg(t.name, ',') within group (order by t.name) tags
from "COMMENT" c
left join COMMENT_TAG ct on ct.id_comment = c.id
left join TAG t on t.id = ct.id_tag
group by c.id, c.text
子句:
ORDER BY
或:
order by c.text
请参见demo。