mySQL中的AND语句

本文关键字:语句 AND 中的 mySQL | 更新日期: 2023-09-27 18:26:16

我正在尝试使用C#MySQL数据库中运行查询。当前面的3列在特定值内时,我希望返回prioritySettings列。这个查询有效,但一次只能使用一个变量,所以目前我只能指定其中一列的值。我的AND语句正确吗?

string query = "SELECT prioritySetting FROM {DATABASE} WHERE handling ='" + handling + "'" + "AND corner ='" + corner + "'" + "AND power  ='" + power + "'";

更多的代码;

                 MySqlDataReader sqlReader;                                       
                string handling = overOrUnderInput;
                string corner = cornerPartInput;
                string power = onOrOffPowerInput;
                string query = "SELECT prioritySetting FROM {DATABASE} WHERE handling ='" +             handling + "'" + " AND corner ='" + corner + "'" + " AND power  ='" + power + "'"; 
                MySqlCommand getRecords = new MySqlCommand(query, connection);
                connection.Open();
                sqlReader = getRecords.ExecuteReader();
                    while (sqlReader.Read())
                    {
                        try
                        {
                            try
                            {
                                suggestions[i] = (sqlReader.GetString(0));
                            }
                            catch
                            {
                            }
                            i++;
                        }
                        catch (MySqlException ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

以及错误;

System.NullReferenceException:对象引用未设置为对象的实例。

mySQL中的AND语句

看起来每个AND之前都需要一个空格。

在每个AND 之前添加一个空格

string query = "SELECT prioritySetting FROM {DATABASE} WHERE handling ='" + handling + "'" + " AND corner ='" + corner + "'" + " AND power  ='" + power + "'";

让我们看看如果插入了参数,查询会是什么样子。我假设参数的值等于它们的名称。

SELECT prioritySetting 
FROM {DATABASE} 
WHERE handling ='handling'AND corner ='corner'AND power  ='power'

如果它真的是FROM之后的DATABASE,则应该将其更改为TABLE。我不太确定它是否会引起任何问题,但我强烈建议在AND s之前放空格。所以你的代码行会像:

string query = "SELECT prioritySetting FROM {DATABASE.TABLE} WHERE handling ='" + handling + "'" + " AND corner ='" + corner + "'" + " AND power  ='" + power + "'";
string query = "SELECT prioritySetting FROM {TABLE}
                    WHERE handling ='" + handling + "'" + " 
                    AND corner ='" + corner + "'" + "
                    AND power  ='" + power + "'";