简体   繁体   English

sql select语句的字段具有多个值

[英]sql select statement with field having multiple values

I have few check-boxes on my selection screen. 我的选择屏幕上没有几个复选框。 Each check-box corresponds to a value which a field of a table can take. 每个复选框对应一个表字段可以采用的值。 I have to then fire a select query where a particular field of a table can have all values whose corresponding check-box is selected. 然后,我必须触发一个选择查询,在该查询中,表的特定字段可以具有其对应的复选框处于选中状态的所有值。

Suppose I have 5 check-box corresponding to values a1-a5. 假设我有5个对应于值a1-a5的复选框。 Now if check-boxes 1, 3, and 4 are checked then the field of the table can have values a1 or a3 or a4. 现在,如果选中了复选框1、3和4,则表的字段可以具有值a1或a3或a4。

select * from table where field = a1 or field = a2 or field = a3.

One way to do this is creating 5 variables and then doing something like this 一种方法是创建5个变量,然后执行类似的操作

if checkbox1 checked
  then var1 = a1
else
  var1 = '0'    //something which would never occur in the field of the table

and so on for all check-boxes. 对于所有复选框,依此类推。

And then 接着

select * from table where field = var1 or field = var2 or field = var3 or field = var4 or field = var5.

This becomes difficult if there are 15 check-boxes. 如果有15个复选框,这将变得很困难。 Is there a better way to do this? 有一个更好的方法吗?

Use a select-option/range table for this: 为此,请使用选择选项/范围表

DATA field_range TYPE RANGE OF data_type_of_table_field.

IF p_check1 = abap_true.
  field_range = VALUE #( BASE field_range ( sign = 'I' option = 'EQ' low = 'A1' ) ).
ENDIF.
IF p_check2 = abap_true.
  field_range = VALUE #( BASE field_range ( sign = 'I' option = 'EQ' low = 'A2' ) ).
ENDIF.
" ...

SELECT whatever FROM wherever WHERE field IN field_range.

CAUTION: An empty range table will match anything ("no restrictions") and fetch the entire contents of the database table, so you'll usually need to check for this separately. 注意:空范围表将匹配任何内容(“无限制”)并获取数据库表的全部内容,因此通常需要单独检查。

这样尝试

select * from table where field IN (a1,a2,a3 ...)

一种可能的方法:将检查值附加到内部表,然后在select语句中使用FOR ALL ENTRIES。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM