2010-08-15 67 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