输入((A+B)-C)*D 式的检索式,如何转化成Delphi 可用的 SQL ( 积分: 100 )

  • 主题发起人 主题发起人 himoo
  • 开始时间 开始时间
H

himoo

Unregistered / Unconfirmed
GUEST, unregistred user!
求个函数。。。<br>+代表或&nbsp;,-代表否,*代表和<br>比如用户可以输入:<br>1.&nbsp;&nbsp;火车+汽车<br>2.&nbsp;&nbsp;火车+汽车-飞机<br>3.&nbsp;&nbsp;(火车+汽车)*飞机<br>4.&nbsp;(飞机*大炮)+(小米*步)<br>等等(检索式不带序号和后面的点、空格)<br><br>通过一个函数&nbsp;可以转化成:<br><br>1.&nbsp;&nbsp;&nbsp;SQL.text='Select&nbsp;count(*)&nbsp;from&nbsp;table&nbsp;where&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'火车'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'&nbsp;or&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'汽车'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'<br><br><br>2.&nbsp;&nbsp;SQL.text='Select&nbsp;count(*)&nbsp;from&nbsp;table&nbsp;where&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'火车'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'&nbsp;or&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'汽车'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'&nbsp;and&nbsp;not&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'飞机'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'<br><br>3.&nbsp;&nbsp;SQL.text='Select&nbsp;count(*)&nbsp;from&nbsp;table&nbsp;where&nbsp;(CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'火车'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'&nbsp;or&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'汽车'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')')&nbsp;and&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'飞机'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'<br><br><br>4.&nbsp;&nbsp;&nbsp;SQL.text='Select&nbsp;count(*)&nbsp;from&nbsp;table&nbsp;where&nbsp;(CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'飞机'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'&nbsp;and&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'大炮'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')')&nbsp;or&nbsp;(CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'小米'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')'&nbsp;and&nbsp;CONTAINTS(field,&nbsp;'&nbsp;+&nbsp;#39&nbsp;+&nbsp;'步'&nbsp;+&nbsp;#39&nbsp;+&nbsp;')')
 
sql生成器
 
很简单啊。因为ABCDE都是表达式,直接用就行了。<br>把“+”号用“&nbsp;or&nbsp;”代替,“-”号用“&nbsp;and&nbsp;not&nbsp;”,“*”用“&nbsp;and&nbsp;”。当然表达式还要考虑一下优先级,就变成这样<br>'Select&nbsp;count(*)&nbsp;from&nbsp;table&nbsp;where&nbsp;((A&nbsp;or&nbsp;B)&nbsp;and&nbsp;not&nbsp;C)&nbsp;and&nbsp;D
 
后退
顶部