2011-11-20 133 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