使用Mysql

时间:2018-02-28 09:47:56

标签: mysql sql

我使用以下查询:

select a,b,c,d from Table1 where a in ('');

查询给出了如下输出:

a    b    c      d
1    A    1e@c   i-1
2    B    #3ht   i-2
3    A    1e@c   Null
4    C    1e@c   

现在我想对那些d为空或空白的记录应用 IF条件

如果我找到与d值或b值对应的任何c值并在其他列e中显示,我想检查整个数据库。

期望的输出:

a    b    c      d     e
1    A    1e@c   i-1   
2    B    #3ht   i-2
3    A    1e@c   Null  i-1
4    C    1e@c         i-1

4 个答案:

答案 0 :(得分:3)

您可以将import { Component, OnInit, Input } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import { AngularFireDatabase } from 'angularfire2/database'; import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore'; import { Expense } from '../expenseModel'; @Component({ selector: 'app-view-expense', templateUrl: './view-expense.component.html', styleUrls: ['./view-expense.component.scss'] }) export class ViewExpenseComponent implements OnInit { expenseId: any; expensesCollection: AngularFirestoreCollection<Expense>; expenses: Observable<Expense[]>; expense: Observable<Expense>; constructor(private db: AngularFirestore, private route: ActivatedRoute) { this.route.params.subscribe(params => { console.log(params); this.expenseId = params.expenseId; }) this.expensesCollection = this.db.collection('/expenses'); this.expenses = this.expensesCollection.snapshotChanges().map(changes = { return changes.map(a => { const data = a.payload.doc.data() as Expense; data.id = a.payload.doc.id; return data; }) }) } ngOnInit() { this.getExpense(); } getExpense() { /* ---- ERROR HERE ---- */ this.expense = this.expensesCollection.where('expenseId', '==', this.expenseId); console.log('this.expense: ' + this.expense); } } 表达式与相关子查询一起使用:

CASE

如果您有多个匹配项,则可以使用SELECT t1.a, t1.b, t1.c, t1.d, CASE WHEN t1.d IS NULL OR t1.d = '' THEN (SELECT t2.d FROM Table1 AS t2 WHERE t2.c = t1.c LIMIT 1) END AS e FROm Table1 AS t1 WHERE t1.a IN (1, 2, 3, 4); 随意选择其中一个匹配项。否则使用一些谓词来选择所需的匹配记录。

Demo here

答案 1 :(得分:0)

您可以使用SELECT IFNULL(d,'1-1') as eSELECT IF((d = ''),'1-1','') as eSELECT IF((d IS NULL) || (d = ''),'1-1','') as e

答案 2 :(得分:0)

试试这个:

@import '~angular-tree-component/dist/angular-tree-component.css'; 

答案 3 :(得分:0)

您可以使用CASE WHEN "Condition" THEN "do this"。 它基本上等同于SQL中的if语句。