Home>

How can I make an exact match and a prefix match compatible?

I want to output matching data from the database
For example
seikana/meikana
Heno/Taro
Henoda/Taro
Henoda Moheji/Taro
Tokyo/Taro
There is data called.

Currently
Prepared fields called seiKana and meiKana for beans
Enter and store in the text box.

And this is done
Service.java

if (bean.seiKana.equals (""))
  {
    bean.seiKana = "%";
  } else
  {
    bean.seiKana = bean.getSeiKana () + "%";
  }
if (bean.meiKana.equals (""))
  {
    bean.meiKana = "%";
  } else
  {
    bean.meiKana = bean.getMeiKana () + "%";
  }


Text box seiKana is empty string
When the text box meiKana is Taro
Heno Taro
Henoda Taro
Henoda Moheji Taro
Output from Tokyo Taro

Text box seiKana is "he"
When the text box meiKana is Taro, it is a prefix match of "he"
Heno Taro
Henoda Taro
Henoda Moheji Taro outputs // Tokyo Taro disappears

Here the text box seikana is "Heno"
When the text box meiKana is Taro
In the current state, there is a prefix match process for "Heno".
Heno Taro
Henoda Taro
Henoda Moheji Taro
Is output,

I want to output only Heno Taro

If there is exact match data, the prefix match process cannot be performed and the process ends there.
I want to.

Will it be like this?
Service.java

if (bean.seiKana.equals (""))
  {
    bean.seiKana = "%";
  } else if (bean.seiKana.equalse (bean.seiKana))
  {
    bean.seiKana = bean.getSeiKana ();
  } else
  {
    bean.seiKana = bean.getSeiKana () + "%";
  }
  • Answer # 1

    For the time being, I will write that it seems to behave like that in SQL
    Since I wrote it in SQL Server, I wondered if the writing method is different for other DBs

    SELECT *
    FROM tablename
    WHERE seikana LIKE COALESCE ((SELECT TOP 1 seikana)
     FROM tablename WHERE seikana ='Heno'),'Heno' +'%')

    Why don't you write the process so that the entered character string is entered in the "Heno" part?

    However, there may be a better way to write it.