2014-04-03 83 views
1

我有兩個表中獲取價值 首先是tbcategory enter image description here如何從SQL Server中的兩個表

二是tbcompany enter image description here

我想從兩個表中這樣的方式檢索常見的圖像從tbcompany並根據外鍵

這裏從tbcategory的所有數據是一個例子,我想

enter image description here

這裏我查詢的SQL Server

select cat.category, comp.imagename from tb_category as cat 
    inner join tb_company as comp 
    on cat.companyid=comp.id 

///////

i am getting result like this 

enter image description here

and want result like this 

enter image description here

+0

,似乎是從查詢失蹤,根據我的理解,只可能是除了選擇其他字段選擇cat.url。你能解釋你的查詢返回什麼,你想要它返回什麼? –

+0

我想公司的單一形象,並在該圖像下應該有關於該公司的類別現在它顯示所有徽標和類別 –

+1

你能解釋一下,通過編輯你的文章,你得到一個樣本結果集,結果集想獲得?我仍然不明白缺少的是什麼。 –

回答

1

基於相當多的假設 - 我們認爲您的數據模式不正確。

你想擁有tbcategory有每個類別

tbcategory: id, category, url 

和tbcompany一個記錄有該公司的類別參考

tbcompany: id, categoryid, name, imagename 

然後你的查詢是

select comp.imagename, cat.category, cat.url 
from tb_company comp 
inner join tb_category cat on comp.categoryid = cat.id 

這將返回如下所示的數據,這似乎是你想要的:

imagename category url 
comp1logo cat1  http://cat1url 
comp2logo cat1  http://cat1url 
comp3logo cat2  http://cat2url 
1

試試這個:

select comp.imagename, cat.category 
    from tb_company comp 
    join tb_category cat on cat.companyid=comp.id 
0
select comp.imagename, cat.category 
    from tb_company comp 
join tb_category cat on cat.companyid=comp.id