用OR语句选择

时间:2019-12-05 07:55:13

标签: sql postgresql postgresql-9.5

我想获取以下数据:如果:Code不为NULL,我想与所有4个条件匹配:Code,Contactor,Type和Nomenclature。如果:Code为NULL,我只想匹配Contractor,Type和Nomenclature。我在这里写了一些查询

SELECT "Nomenclature" as "IdNom",
       "Cod"          as "ContrCode",
       "Contanctor"
FROM "КодНоменклатуры"

WHERE (:Code IS NOT NULL OR "Code" = ANY (:Code :: text[]))

  AND ("Contactor" = :Contractor::integer
  AND "Type" = :IsSupplier::integer
  AND "Nomenclature" IS NOT NULL)

但是它不能正常工作。当我输入代码时,它将查询与承包商,类型和术语匹配但与代码不匹配的所有内容。如果我没有输入代码,则结果是正确的,我匹配了Contractor,Type和Nomenclature。

3 个答案:

答案 0 :(得分:0)

SELECT "Nomenclature" AS "IdNom", "Cod" AS "ContrCode", "Contanctor"
FROM "КодНоменклатуры"
WHERE (
        :Code IS NOT NULL AND "Contactor" = :Contractor::integer AND "Type" = IsSupplier::integer AND "Nomenclature" IS NOT NULL
      ) 
      OR 
      (
        "Code" = ANY (:Code :: text[]) AND "Contactor" = :Contractor::integer
      )

答案 1 :(得分:0)

只要您将内容传递给:Code,

component.parent.form = mockFormGroup; 始终将评估为TRUE。因此,import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { Component, NO_ERRORS_SCHEMA } from '@angular/core'; import { ReactiveFormsModule, FormGroupDirective, FormControlDirective, FormBuilder, ControlContainer, FormGroup, FormControl } from '@angular/forms'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture<MyComponent>; let formGroupDirective: FormControlDirective; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ MyComponent ], imports: [ HttpClientTestingModule, ReactiveFormsModule ], providers: [ FormGroupDirective, { provide: ControlContainer, useValue: formGroupDirective } ], schemas: [NO_ERRORS_SCHEMA] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; //mock parent formGroup const mockFormGroup: FormGroup = new FormGroup({ }); //dummy formgroupDirective to avoid undefined addControl function const formGroupDirective: FormGroupDirective = new FormGroupDirective([], []); component.parent.form = mockFormGroup; component.ngOnInit(); fixture.detectChanges(); }); it('should be created', () => { expect(component).toBeTruthy(); }); });之后的部分将不再进行评估,因为整个术语为TRUE。

鉴于您正确传递了一个数组Literal,以便强制转换为text []才能正确执行查询

...WHERE :Code IS NOT NULL...

提到这条线 在哪里(:Code不为空并且“ Code” = ANY(:Code :: text []))

答案 2 :(得分:0)

答案是: 哪里   (((:Code IS NULL或:Code ='{}')或“ Code” = ANY(:Code :: text []))