SQL:从另一个数据集添加多个列

时间:2018-05-21 11:15:25

标签: mysql sql

我有一个像这样结构的数据集export class storeProfileComponent implements OnInit { storeProfile: any = {}; userData: any = {}; userId: any; createStoreForm: FormGroup; formSubmitAttempt: boolean; constructor(public fb: FormBuilder, private storeSrv: StoreService, private router: Router) { } ngOnInit() { let tempUser = localStorage.getItem('user'); if (tempUser) { this.userData = JSON.parse(tempUser); this.userId = this.userData.id; }; this.createStoreForm = this.fb.group({ 'user': [this.userId.id, Validators.required], }); } } <form [formGroup]="createStoreForm" (ngSubmit)="createStore()" novalidate> <label for="user">Unique User ID</label> <input type="text" name="user" class="form-control" formControl="user" readonly> <div *ngIf="createStoreForm.controls['user'].hasError('required') && formSubmitAttempt" class="form-error">Store user is required</div> </form>

train

+---+---+---+----------+----------+----------+ | a| b| c|aml_freq_a|aml_freq_b|aml_freq_c| +---+---+---+----------+----------+----------+ | v| l| l| 56| 42| 29| | u| g| l| 47| 46| 29| | s| g| l| 28| 46| 29| | v| m| l| 56| 33| 29| | h| m| l| 44| 33| 29| +---+---+---+----------+----------+----------+ aml_freq_aaml_freq_b列是aml_freq_ca, b列中相应条目的频率。例如,c下的值56基本上是aml_freq_av出现次数的计数/频率。同样适用于ab

我有另一个名为c的数据集,其中只有testa, b列,

c

我需要添加+---+---+---+ | a| b| c| +---+---+---+ | w| j| c| | a| g| w| | s| d| i| | g| j| r| | r| b| u| +---+---+---+ aml_freq_aaml_freq_b列。为此,我使用了加入aml_freq_c列上的testtrain的子查询,然后选择了a列。我为aml_freq_ab再写了两个子查询。这就是我的查询的外观

c

但是当我运行它时,我收到以下错误

SELECT test.*, 
       (SELECT aml_freq_a 
        FROM   test 
               LEFT JOIN train 
                      ON test.a = train.a), 
       (SELECT aml_freq_b 
        FROM   test 
               LEFT JOIN train 
                      ON test.b = train.b), 
       (SELECT aml_freq_c 
        FROM   test 
               LEFT JOIN train 
                      ON test.c = train.c) 
FROM   test 

我没有理解这个错误。由于我在more than one row returned by a subquery used as an expression 左侧有left join,如何有更多行?

任何方向都会很棒。

PS:我读过有关同一问题的其他帖子,但是他们达到这个错误的方式与我正在做的不同,那里的解决方案对我没什么帮助。 < / p>

1 个答案:

答案 0 :(得分:3)

我认为你只需要三个连接,一个用于train表中的每个频率列:

SELECT
    t.a,
    t.b,
    t.c,
    tr1.aml_freq_a,
    tr2.aml_freq_b,
    tr3.aml_freq_c
FROM test t
LEFT JOIN train tr1
    ON t.a = tr1.a
LEFT JOIN train tr2
    ON t.b = tr2.b
LEFT JOIN train tr3
    ON t.c = tr3.c

修改

如果您想采用当前的方法,那么这些方面的内容可能会有效:

SELECT
    t.*, 
   (SELECT aml_freq_a FROM train tr WHERE t.a = tr.a) aml_freq_a,
   (SELECT aml_freq_b FROM train tr WHERE t.b = tr.b) aml_freq_b,
   (SELECT aml_freq_c FROM train tr WHERE t.c = tr.c) aml_freq_c
FROM test t;

我说可能,因为这些子查询必须始终返回单个值以避免错误。即使这样可行,我给出的连接方法也应该优于它。