我可以从同一列中选择多个值吗?

 收藏

我试图使用一个MySql.Data SELECT语句从多个行中获取值。

我尝试使用以下代码,但失败了。该错误表明行#上存在语法错误。

enter image description here

MySqlConnection mysqlcon = new MySqlConnection(M_str_sqlcon);

MySqlCommand mysqlcom = new MySqlCommand(
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'SKU') AND " +
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'Price') AND " +
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'Sold') AND " +
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'SoldDate')"
, mysqlcon);

MySqlDataReader r = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);

prodMeta.Add(new ProductMeta{
    ProductSKU = r.GetString(0),
    RegularPrice = r.GetDouble(1),
    SoldCount = r.GetInt32(2),
    SoldDate = r.GetString(3)
});

mysqlcon.Close();
回复
  • Firstly you can use the in keyword instead:

    SELECT value FROM metatable WHERE ProductID = 16 AND MetaKey in ('SKU','Price', 'Sold', 'SoldDate')
    

    其次,您不需要在后面加上括号。