0

私は結婚大学のプロジェクトで高度な検索を行いました。その結果、次のクエリがありました。

select * 
from tbl_FreeUserProfile 
where Gender='Male' and Age>= '18' 
  and Age<= '40'    and heightf<='182' 
  and heightf>='152' and MaritalStatus='Never Married' 
  and MotherTongue = 'xyz' or 'Gujarati' or 'Urdu' or 'Hindi'"

しかし、クエリを実行すると、次のエラーが表示されます。

'または'の近くの条件が予想されるコンテキストで指定された非ブール型の式

誰かがSQLクエリの形式の何が問題になっているのか教えてもらえますか?

私のコーディングはこんな感じです

protected void ImageButton11_Click(object sender, ImageClickEventArgs e)
  {

  string sql = "select * from tbl_FreeUserProfile where Gender='" + RadioButtonList2.SelectedItem.Text + "' and Age>= '" + DropDownList25.SelectedItem.Text + "' and Age<= '" + DropDownList26.SelectedItem.Text + "' and heightf<='" + TextBox23.Text + "' and heightf>='" + TextBox22.Text + "' and MaritalStatus='" + DropDownList35.SelectedItem.Text + "'";

  if (ListBox2.Items.Count >= 0)
    {
        sql = sql + " and MotherTongue = 'xyz'";

        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {                
              string mt = ListBox2.Items[i].ToString();
              sql = sql + " or '" + mt + "'";              
        }          
    }
4

3 に答える 3

3

私はあなたが次のようなことをしたかったと思います

and MotherTongue IN ('xyz', 'Gujarati', 'Urdu', 'Hindi')

使用するor場合は、毎回フィールド名を指定する必要があります。

于 2012-04-21T17:30:55.700 に答える
2

あなたはこれを行うことができます

AND (MotherTongue = 'xyz' OR MotherTongue = 'abc' ...)

コードをに変更する

if (ListBox2.Items.Count >= 0)
{
    sql = sql + " and ( MotherTongue = 'xyz'";

    for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
    {                
          string mt = ListBox2.Items[i].ToString();
          sql = sql + " or MotherTongue = '" + mt + "'";              
    }

    sql = sql + ")"
}

また

AND MotherTongue IN ('xyz', 'abc', ...)

コードをに変更する

if (ListBox2.Items.Count >= 0)
{
    sql = sql + " and MotherTongue IN (" + 
          string.Join(",", ListBox2.Items[i].Select(i => i.ToString())) + ")";
}
于 2012-04-21T17:55:02.750 に答える
0

現在、実行中のクエリは次のように終了します。

and MotherTongue = 'xyz' or 'Gujarati' or 'Urdu'... so on. 

実際のクエリは次のようになります。

and MotherTongue = 'xyz' or MotherTongue =  'Gujarati' or MotherTongue = 'Urdu'

複数の言語を照会する最良の方法は、C.Evenhuisが述べたように、つまり次のように使用することです。

and MotherTongue IN ('xyz', 'Gujarati', 'Urdu', 'Hindi').
于 2012-04-21T17:55:28.083 に答える