2009-05-23 69 views
1

我正在開發使用基於Joomla的數據庫的CMS!在的Joomla分貝,我們有2個表:在包含相同字段名的兩個表上選擇?

+----------+ 
|Categories| 
+----------+ 
id 
title 
... 

+-------+ 
|Content| 
+-------+ 
id 
title 
catid 
... 

我有下面的查詢:

SqlQuery q = new Select("*") 
         //.Top("1") 
         .From(JosContent.Schema) 
         .InnerJoin(JosUser.IdColumn, JosContent.CreatedByColumn) 
         .InnerJoin(JosCategory.IdColumn, JosContent.CatidColumn) 
         .Where("catid").IsEqualTo(catId); 

而在ASPX頁面,我展示的數據這樣的:

Tite : <%# DataBinder.Eval(Container.DataItem, "title") %> 
In category : <%# DataBinder.Eval(Container.DataItem, "title") %> 
// Category tite not Content title, but ASP.NET think it is Content title :-(

請幫助我修復它?如何在這之間進行分離?

非常感謝!

+3

不要使用select *,不好的做法。 – grepsedawk 2009-05-23 16:10:34

回答

4

您可以參考titleCategories的表:Categories.titletitleContent表由:Content.title。對不起,如果我誤解了你的問題。

1

在你的選擇中,你可以像Alan說的那樣做,然後用AS來改變你以後引用它們的內容。 (我不明確地知道ASP,我是一名PHP程序員,但我認爲它相當類似)。

喜歡的東西

SELECT *,Categories.title AS categoryTitle,Content.title AS contentTitle ... ...

然後你可以參考categoryTitle或contentTitle。

+0

嗨, 我從2003年到2007年的PHP工作,但現在我對ASP.NET感興趣:-) 我知道你上面發佈的查詢,但現在我使用SubSonic(http://subsonicproject.com, http://subsonicproject.com/querying/select-queries/) - .NET的數據訪問層。正如你所看到的,我上面發佈的查詢不像傳統的SQL查詢。 我只想知道如何在SubSonic中編寫SQL,而不是傳統的SQL。 謝謝! – Shinichi 2009-05-23 16:22:44

0

我已經finised吧:)

SqlQuery q = new Select("*", "jos_Categories.title AS 'CatTitle'") 
         //Select("*", "CatTitle = jos_Categories.title") 
         //Select("*", "CatTitle = JosCategory.TitleColumn") 
         //.Top("1") 
         .From(JosContent.Schema) 
         .InnerJoin(JosUser.IdColumn, JosContent.CreatedByColumn) 
         .InnerJoin(JosCategory.IdColumn, JosContent.CatidColumn) 
         .Where("catid").IsEqualTo(catId); 

謝謝...谷歌:-)

相關問題