Can I SELECT more than one value from the same column

Lee Denbigh :

I'm trying to get values from more than one row using a single MySql.Data SELECT Statement.

I tried using the below code but failed. The error says that

there is a syntax error on line #

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();
ggordon :

You could modify your query to UNION all the output as shown below. The Union combines the results of 2 or more queries (ensure that you have similar columns in the same order)

MySqlConnection mysqlcon = new MySqlConnection(M_str_sqlcon);

MySqlCommand mysqlcom = new MySqlCommand(
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'SKU') UNION " +
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'Price') UNION " +
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'Sold') UNION " +
    "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();

However, since you are pulling your data from the same table in this example, you could use an OR to combine the conditions as below:

MySqlConnection mysqlcon = new MySqlConnection(M_str_sqlcon);

MySqlCommand mysqlcom = new MySqlCommand(
    "SELECT value FROM metatable WHERE(ProductID = 16 AND MetaKey = 'SKU')  OR (ProductID = 16 AND MetaKey = 'Price') OR (ProductID = 16 AND MetaKey = 'Sold') OR (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();

or you could use the IN operator as below:

MySqlConnection mysqlcon = new MySqlConnection(M_str_sqlcon);

MySqlCommand mysqlcom = new MySqlCommand(
    "SELECT value FROM metatable WHERE ProductID = 16 AND MetaKey IN ('SKU','Price','Sold','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();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360004&siteId=1