当我从多个表中选择时,如何通过分组前实现订单

时间:2015-12-03 09:39:37

标签: php mysql

我尝试使用php从mysql表中检索数据。我的代码是这样的:

$sql="SELECT t.tagname, v.videotitle, v.videolength ,f.frametime, f.videoloc FROM video v, tag t, frame f, framename fn WHERE t.tagid = fn.tagid and fn.frameid=f.frameid and v.videoloc=f.videoloc AND tagname LIKE '".$value."' group by fn.section";

标记名录像带视频长度帧时间
pc ---------- test ------- 00:00:03 ----- 1 / 0.17s
pc ----------测试-------- 00:00:03 ----- 6 / 1.07s

f.frametime列不是我想要的最小数字,因为我的数据库顺序是这样的。然后我改为:

$sql="SELECT t.tagname, v.videotitle, v.videolength ,f.frametime, f.videoloc FROM video v, tag t, frame f, framename fn WHERE t.tagid = fn.tagid and fn.frameid=f.frameid and v.videoloc=f.videoloc AND tagname LIKE '".$value."' order by f.frametime";

标记名录像带视频长度帧时间
PC ----------- --------测试-------- 0点00分03秒1 / 0.17s
PC ----------- --------测试-------- 0点00分03秒2 / 0.35S
PC ----------- --------测试-------- 0点00分03秒3 / 0.53s
PC ----------- --------测试-------- 0点00分03秒5 / 0.89s
pc ----------- test -------- 00:00:03 -------- 6 / 1.07s

BTW ,第1,2,3帧具有相同的部分ID 1,第5,6帧具有部分ID 2。 这给了我正确的订单,但它们没有组合在一起。如果我可以先通过f.framename然后按fn.section分组,我的问题就会解决。但语法不正确。

我想要实现的是:

标记名录像带视频长度帧时间
pc ---------- test ------- 00:00:03 ----- 1 / 0.17s
电脑----------测试-------- 00:00:03 ----- 5 / 0.89s

任何想法? 感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

基于Rahautos的答案,我解决了它 他的答案给了我#1054 - 'order clause'中的未知列'f.framename',因为我没有在原始陈述中选择'section'列。
所以我把它添加到:

Select tagname, videotitle, videolength, frametime, videoloc From ( SELECT t.tagname, v.videotitle, v.videolength ,f.frametime, f.videoloc, fn.section FROM video v, tag t, frame f, framename fn WHERE t.tagid = fn.tagid and fn.frameid=f.frameid and v.videoloc=f.videoloc AND tagname LIKE '".$value."' order by f.frametime) new_table group by new_table.section

这给了我我想要的东西,即使mysql表中的数据不合适。

答案 1 :(得分:0)

尝试此查询

Select * From (
SELECT t.tagname, v.videotitle, v.videolength 
,f.frametime, f.videoloc 
FROM video v, tag t, 
frame f, framename fn 
WHERE t.tagid = fn.tagid and fn.frameid=f.frameid 
and v.videoloc=f.videoloc 
AND tagname LIKE '".$value."' order by f.frametime
) new_table
group by new_table.section