如何在同一个表上选择两次在一个查询或往返?

时间:2013-08-07 10:38:57

标签: mysql c#-4.0

好的,这是我尝试过的代码 -

    private static void CheckForChanges(MySqlConnection connection)
    {
        DateTime hair = DateTime.Parse(SmartStyleDataset.Tables["Hair"].Compute("MAX(lastUpdated)", null).ToString());
        DateTime cloths = DateTime.Parse(SmartStyleDataset.Tables["Cloths"].Compute("MAX(lastUpdated)", null).ToString());
        DateTime accessories = DateTime.Parse(SmartStyleDataset.Tables["Accessories"].Compute("MAX(lastUpdated)", null).ToString());
        DateTime cosmetics = DateTime.Parse(SmartStyleDataset.Tables["Cosmetics"].Compute("MAX(lastUpdated)", null).ToString());

        List<String>[] NewRows = { new List<String>(), new List<String>(), new List<String>(), new List<String>() };   

        using (var command = connection.CreateCommand())
        {
            connection.Open();
            command.CommandText = 
                 "SELECT * "
                + "FROM smartstyle.table_changes_logger "
                + "WHERE  Updated > @accessories AND Table = 'accessories' ; "
                + "SELECT * "
                + "FROM smartstyle.table_changes_logger "
                + "WHERE  Updated > @cosmetics AND Table = 'cosmetics' ; "
                + "SELECT * "
                + "FROM smartstyle.table_changes_logger "
                + "WHERE  Updated > @cloths AND Table = 'cloths' ; "
                + "SELECT * "
                + "FROM smartstyle.table_changes_logger "
                + "WHERE  Updated > @hair AND Table = 'hair'";

            command.Parameters.Add("@cloths", MySql.Data.MySqlClient.MySqlDbType.Timestamp).Value = cloths.ToString("yyyy-MM-dd HH:mm:ss");
            command.Parameters.Add("@hair", MySql.Data.MySqlClient.MySqlDbType.Timestamp).Value = hair.ToString("yyyy-MM-dd HH:mm:ss");
            command.Parameters.Add("@accessories", MySql.Data.MySqlClient.MySqlDbType.Timestamp).Value = accessories.ToString("yyyy-MM-dd HH:mm:ss");
            command.Parameters.Add("@cosmetics", MySql.Data.MySqlClient.MySqlDbType.Timestamp).Value = cosmetics.ToString("yyyy-MM-dd HH:mm:ss");

            using (var reader = command.ExecuteReader())
            {
                do
                {
                    while (reader.Read())
                    {
                        NewRows[0].Add(reader.GetString(1));
                        NewRows[1].Add(reader.GetString(2));
                        NewRows[2].Add(reader.GetString(3));
                        NewRows[3].Add(reader.GetString(4));
                    }

                } while (reader.NextResult());

            }
        }
 }

但这不起作用我从mysql得到以下一般错误我不明白 您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在'Table ='accessories'附近使用正确的语法; SELECT * FROM smartstyle.table_changes_logger WHERE Upd'在第1行

1 个答案:

答案 0 :(得分:1)

您需要使用反引号转义reserved words in MySQL table

SELECT * 
FROM smartstyle.table_changes_logger 
WHERE  Updated > @accessories 
AND `Table` = 'accessories'