如何编写这个SQL语句?

时间:2012-09-17 14:05:50

标签: asp.net sql database visual-studio-2010

我有两张桌子:

  1. 活动:

    -id;名称; startevent; ENDEVENT;是PersonID;

  2. RegistracijaKorisnik:

    -id; IME;

  3. 现在我写了这个:

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

    只有这个显示名称,eventstart,eventend。现在我想显示Person表中的名称和名称,eventstart,偶然从事件。在Event表中,我想将EventID中的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 个答案:

答案 0 :(得分: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 ))

答案 1 :(得分: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))

答案 2 :(得分: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)

答案 3 :(得分:0)

两个表中有相同的列名。

解决此问题的原因是

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

  • 您可以使用alias lik 选择事件为e

哪个应该有用