2011-11-20 134 views
0

這是我第一次在這裏發佈問題。我正在研究一個項目,並且陷入困境。我正在使用codeignitor框架。PHP MYSQL查詢幫助合併數據

我使用php作爲核心語言和MYSQL數據庫。

這是我的問題:

我有3個表temp_frds,如下所示top_frds_temp和尺寸和結構。

 
temp_frds (contains, userid, friend id, friend name, friend picture url) 

id|fid|uid|name|picture 

top_frds_temp (contains friends id and rating of friendship) 

fid|rating 


dimensions (contains picture x,y cordinate and width and hight of image) 

id|XD|YD|WIDTH|HEIGHT 

現在這些數據是用來做什麼的?第一我保存用戶的所有朋友。然後保存用戶的好友並放置評級。

現在我想要運行一個查詢,它將查找排名前50的朋友,名稱,ID和圖片,然後通過維度表的ID從維度表順序中查找所有數據。這裏是exmple我需要在輸出:

 

$array=array(
array('img'=>'IMGURL','fid'=>'243242','name'=>'john','rating=>'50','x'=>'5','y'=>'5','w'=>'200','h'=>'200'), 
array('img'=>'IMGURL','fid'=>'245545','name'=>'mike','rating=>'43','x'=>'5','y'=>'100','w'=>'200','h'=>'200'), 
array('img'=>'IMGURL','fid'=>'265544','name'=>'mark','rating=>'30','x'=>'5','y'=>'195','w'=>'200','h'=>'200'), 

); 

更多信息:

temp_frds.id和dimensions.id沒有關係。查詢應根據top_frds_temp中的評級獲取頂級好友,然後從temp_frds獲取名稱和URL信息,然後從維度表中添加維度。

例如:

加入temp_frds,top_frds_temp我

 


uid|name|url 
1 john URL 
6 mike URL 
10 mark URL 


後,現在如果在表漁政表中的數據是這樣的

 
id|XD|YD|WIDTH|HEIGHT 
1 5 5 200 200 
2 5 100 200 200 
3 60 300 200 200 

最終的結果應該是

 

uid|name|url| id|XD|YD |WIDTH|HEIGHT 

1 john URL 1 5 5 200 200 
6 mike URL 2 5 100 200 200 
10 mark URL 3 60 300 200 200 


回答

0

我不確定是否有你的數據模型。我假設temp_frds.iddimensions.id

那麼這樣的事情應該做的伎倆:

在YOUT維列 HIGHT錯字的
SELECT 
    temp_frds.picture AS img, 
    temp_frds.fid, 
    temp_frds.name, 
    top_frds_temp.rating, 
    dimensions.xd = x, 
    dimensions.yd = y, 
    dimensions.width = w, 
    dimensions.hight = h 

FROM 
    temp_frds 

JOIN 
    top_frds_temp ON top_frds_temp.fid = temp_frds.fid 

JOIN 
    dimensions ON dimensions.id = temp_frds.id 

ORDER BY 
    top_frds_temp.rating DESC 

LIMIT 50 

Beaware,這也許應該是HEIGHT

+0

感謝jona的回答,temp_frds.id和dimensions.id沒有任何關係。我已經編輯了quetion,並提供了更多信息......在更多信息部分 –

+0

@SandyFark然後我沒有得到你的數據庫佈局。尺寸表是如何與模型的其餘部分相關的?您需要一些關係信息將該維與temp_frds表連接起來。 – Jona