1

我有两张桌子:

  1. 事件:

    -ID; 姓名; 开始事件;结束事件;个人ID;

  2. RegistracijaKorisnik:

    -ID; 我;

现在我写了这个:

SqlDataAdapter da = new SqlDataAdapter(
         "SELECT [id], [name], [eventstart], [eventend] " +
         "FROM [Event] " + 
         "WHERE NOT (([eventend] <= @start) OR ([eventstart] >= @end))",
       con);

有了这个唯一的显示名称,eventstart,eventend。现在我想显示来自 Person 表的名称和来自 event 的 name、eventstart、eventend。在 Event 表中,我想将 Event 表中的 PersonID 连接到 Person 表中的 ID 字段。你能帮助我吗?

编辑:所有代码,不知道为什么不显示 p.ime

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek(new DateTime(2012, 09, 17));
            DayPilotCalendar1.DataSource = dbGetEvents(DayPilotCalendar1.StartDate, DayPilotCalendar1.Days);
            DataBind();
        }

    }


 protected DataTable dbGetEvents(DateTime start, int days)
    {

      SqlConnection con = new SqlConnection();
      con.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Korisnik;Integrated Security=True";
      //SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name], [eventstart], [eventend] FROM [Event] WHERE NOT (([eventend] <= @start) OR ([eventstart] >= @end))", con);
     //SqlDataAdapter da = new SqlDataAdapter("select  e.id as EventId, e.name as EventName, e.eventstart, e.eventend, p.Id as PersonId, p.ime as PersonName from Event e inner join RegistracijaKorisnik p on e.PersonID=p.id where not (([eventend] <= @start) OR ([eventstart] >= @end))", con);
      //SqlDataAdapter da = new SqlDataAdapter("SELECT e.id, e.name, e.eventstart, e.eventend, p.ime FROM Event e INNER JOIN RegistracijaKorisnik p ON e.PersonID = p.id WHERE NOT ((e.eventend <= @start) OR (e.eventstart >= @end ))", con);
      SqlDataAdapter da = new SqlDataAdapter("SELECT e.id, e.name, e.eventstart, e.eventend, p.ime AS 'Person Name' FROM Event e INNER JOIN RegistracijaKorisnik p ON e.PersonID = p.id WHERE NOT ((e.eventend <= @start) OR (e.eventstart >= @end ))", con);
      da.SelectCommand.Parameters.AddWithValue("start", start);
      da.SelectCommand.Parameters.AddWithValue("end", start.AddDays(days));

      DataTable dt = new DataTable();
      da.Fill(dt);
      return dt; 


    }
4

4 回答 4

2

JOIN两张表:

SELECT e.id, e.name, e.eventstart, e.eventend,
       p.Name + ' ' + p.Surname AS 'Person Name'
FROM Event e
INNER JOIN Person p ON e. PersonID = p.ID
WHERE NOT ((e.eventend <= @start) OR (e.eventstart >= @end ))
于 2012-09-17T14:09:13.267 回答
2
select 
  e.id as EventId
  ,e.name as EventName
  ,e.startevent
  ,e.endevent
  ,p.Id as PersonId
  ,p.name as PersonName 
  ,p.surname as PersonSurname
  ,p.email
from Event e
join Person p on e.PersonId=p.Id
where not (([eventend] <= @start) OR ([eventstart] >= @end))
于 2012-09-17T14:10:36.137 回答
1

如果我正确理解您的问题,这只是一个简单的连接,如下所示:

SELECT e.id, e.name, e.eventstart, e.eventend, p.name
FROM Event e
   INNER JOIN Person p ON p.Id = e.ID
WHERE NOT ((e.eventend <= @start) OR (e.eventstart >= @end)
于 2012-09-17T14:09:03.567 回答
0

您在两个表中具有相同的列名。

这个问题的解决方法是

  • 您可以在查询中使用完全限定的列名,例如 tablename.columnname - 即 event.id、person.id 等或

  • 您可以使用别名喜欢选择事件作为 e

这应该工作

于 2012-09-17T14:16:15.243 回答