0

I have a table as follows:

Id = stretch(1..100,1000)
area = take(`A`B`C`D`E`F`G`H`I`J,1000)
qyt = rand(100.00,1000)
t  = table(Id,area,qyt)

I want to query data with multiple field values.

Query condition:

  • field value “Id=1“, “area“ = A, F, G;
  • field value “Id=2“, “area“ = B, C, D;
  • field value “Id=3“, “area“ = B, C, G;

I tried the following method, querying each record with a where clause.

select * from t where (Id =1 and area = `A) or (Id = 1 and area = `F) or(Id = 1 and area = `G)

The code is simple but long. Is there any easier way to solve this problem?

jinwandalaohu
  • 226
  • 1
  • 7

1 Answers1

1

The query condition involves field “Id“ and “area“. First, you can add a new field “newCol“ that combines these two fields.

temp = select *,string(Id)+area as newCol from t

Then, conduct a query as follows:

aim_row = `1A`1F`1G`2B`2C`2D`3B`3C`3G
result = select Id,area, qyt from temp where newCol in aim_row

Output:

Id area qyt               
-- ---- ------------------
1  A    30.710145598277449
1  F    37.276206677779555
1  G    81.456008832901716
2  B    47.396290418691933
2  C    68.868489493615925
2  D    72.536952490918338
3  B    77.235422702506184
3  C    32.31446212157607 
3  G    39.283064194023609
YaN
  • 407
  • 1
  • 6