简体   繁体   English

如何选择超过4条条件的记录

[英]How to Select records with over 4 criteria

Select records based on the criteria shown 根据显示的条件选择记录

在此处输入图片说明

Is there any way i can select records from MySQL based on this criteria apart using the many if else statements. 有什么办法可以使用许多if else语句根据此条件从MySQL选择记录。 Actually what i have in mind is below 其实我想的是下面

if CurrentLevel.SelectedItem <> Nothing AND Programme.SelectedItem = Nothing AND Gender.SelectedItem = Nothing Then

myconnection.Open()
Dim SelCmd as SqlCommand = New SqlCommand("Select * From StudentsList Where CurrentLevel = '"& CurrentLevel.SelectedItem &"'",myconnection)

And I'll have to do it for all the possible outcomes. 对于所有可能的结果,我都必须这样做。 Which makes the code very lengthy and tiresome to write. 这使得代码非常冗长且难以编写。

Is there a shorter way of performing this search because I'll perform another search with almost 16 criteria. 有没有一种执行此搜索的较短方法,因为我将使用将近16个条件执行另一个搜索。

The question is not entirely clear to me. 这个问题对我来说并不完全清楚。 You won't need a new if statement for all the criteria. 您不需要所有条件的新if语句。 You can achieve this by modifying the SQL query itself. 您可以通过修改SQL查询本身来实现。 One of the main purpose of SQL is to get data that match certain criteria. SQL的主要目的之一是获取符合特定条件的数据。

SELECT * FROM StudentsList WHERE `CurrentLevel` = "level" AND `gender` = "male" AND `programme` = "something"

The above SQL query should give you a basic idea. 上面的SQL查询应该给您一个基本的想法。 It will select the rows which have CurrentLevel as level, gender as male and programme as something only, the rest will be ignored. 它将选择以下行:CurrentLevel为level,性别为male,仅将program作为某项,其余行将被忽略。

EDIT: 编辑:

I don't know VB. 我不认识VB。 Here is a quick, dirty example in C# which will help you understand the basic logic behind this. 这是C#中一个快速,肮脏的示例,它将帮助您了解其背后的基本逻辑。

string sqlQuery = "SELECT * FROM StudentsList ";
if(currentLevelDropDown.SelectedItem.Text != "") 
{
    sqlQuery + "WHERE CurrentLevel = " + currentLevelDropDown.SelectedItem.Text;
}

if(ProgrammeDropDown.SelectedItem.Text != "")
{
    sqlQuery + " AND WHERE programme = " + ProgrammeDropDown.SelectedItem.Text;
}

//Final Query becomes: SELECT * FROM StudentsList WHERE CurrentLevel = userSelectedOption AND WHERE programme = userSelectedProgrammeOption

//Finally execute the sqlQuery

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

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