2010-08-15 62 views
0

评论我有events表,其中包括这么多的cols除了Id PK
和表Comments,其中包括texteventIdId PK。
我该如何选择事件信息,它是一条单独的sql语句中的注释,如何使用它以及它应该是什么样的!
如何选择每行

注意事件ID(评论)= ID(事件)

+0

你说的“如何意味着什么用它'?你有使用这些数据的应用程序吗?什么样的应用程序?什么编程语言? – 2010-08-15 13:55:04

+0

我正在使用C#... – Rawhi 2010-08-15 13:57:12

回答

5

嗯,这应该去做...

SELECT * FROM events 
INNER JOIN comments on comments.eventid = events.id 

如果您不需要的所有列是很好的做法,只选择列你真的需要。

SELECT Id, eventId FROM events 
INNER JOIN comments on comments.eventid = events.id 

来提取它在你的C#代码,你可能做到这一点System.Data风格:

using (SqlConnection connection = new SqlConnection("Put your ConnectionString here")) 
{ 
    connection.Open(); 
    using (SqlDataAdapter adapter = new SqlDataAdapter("My SQL from Above", connection)) 
    { 
     DataTable table = new DataTable(); 
     adapter.Fill(table); 
     // now you can do here what ever you like with the table... 

     foreach(DataRow row in table.Rows) 
     { 
      if (row.IsNull("Text") == false) 
       Console.WriteLine(row["Text"].ToString()); 
     } 
    } 
} 
+0

我需要知道如何使用和提取它(每个事件都有很多评论) – Rawhi 2010-08-15 13:53:06

0

我会用以下语句:

SELECT e.Id, c.Id, c.text 
FROM events e 
INNER JOIN Comments c ON e.Id = c.EventId