在数据库中打开不同的表

本文关键字:数据库 | 更新日期: 2023-09-27 18:16:41

所以我在MySQL数据库中有3个不同的表,名为"MyDB",里面的表是table1, table2, table3。这是我的代码查询数据库中的3个表,但它不断打破,我不知道为什么。为Google Maps Javascript API v3创建这个

private static IEnumerable<HeatMapDataElement> QueryHeatMapDataFromDatabase(string reqDate, string reportType)
    {
        const string connectionString = "connection_credentials";
        var ds = new DataSet();
        var elements = new List<HeatMapDataElement>();
        var conn = new MySqlConnection(connectionString);
        MySqlCommand cmd;
        var da = new MySqlDataAdapter();
        conn.Open();
        try
        {
            cmd = conn.CreateCommand();
            cmd.CommandTimeout = 30;
            cmd.CommandText =
                "select RequestHour, longitude, latitude, count(requesttime) as Weight from MyDB.table1 || MyDB.table2 || MyDB.table3 where method=@ReportType and requestdate = date(@RequestDate) and longitude is not null and latitude is not null and requesthour is not null group by RequestHour, longitude, latitude";
            cmd.Parameters.AddWithValue("@ReportType", reportType);
            cmd.Parameters.AddWithValue("@RequestDate", reqDate);
            da.SelectCommand = cmd;
            da.Fill(ds);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                elements.AddRange(from DataRow dr in ds.Tables[0].Rows
                                  select new HeatMapDataElement()
                                      {
                                          Latitude = Double.Parse(dr["latitude"].ToString()), Longitude = Double.Parse(dr["longitude"].ToString()), Hour = int.Parse(dr["RequestHour"].ToString()), Weight = int.Parse(dr["Weight"].ToString())
                                      });
            }
        }
        catch (Exception ex)
        {
            throw ex; //breaks here <-------
        }
        finally
        {
            conn.Close();
            da = null;
            cmd = null;
        }
        return elements.AsEnumerable();
    }

错误是:你有一个错误在你的SQL语法;查看与您的MySQL服务器版本对应的手册,以便在'|| MyDB附近使用正确的语法。where method='Table 2' and requestdate =' at line 1 '

在数据库中打开不同的表

你把c#和SQL代码混在一起了。AFAIK管道操作符只能在mysql中用于连接列而不是表,即使这样,默认情况下它是禁用的,所以我认为你需要使用INNER JOIN

如果你从3个表中读取数据,你可以在3个表之间做一个内部连接,就像这样:

 select * from table1 inner join table2 on table1.key = table2.key