2012-03-22 73 views
0

爵士怎样才能通过使用组由

添加两个不同的ID我有两个表

第一个名字screen_main有场

screen_main_id,screen_date,start_date,end_date 
1 12 15 
2 13 15 
3 14 16 

我有两个起点之间取一个screen_main_id日期和结束日期后,我有另一个表名screen_product表

field:

screen_id screen_main_id article_id quantity price %age 
1   1    1   5   234 12% 
2   2    1   6   221 12% 
3   1    2   5   111 10% 

现在第一张桌子给我screen_main_id当我选择开始日期和结束日期后选择screen_main_id我必须总结所有的数据具有artical组明智的就像artical = 1那么它做的总和我们得到的两个id从第一张桌子。

我的回答

用于选择第一screen_main_id

select * from screen_main where screen_date between start_date='$start_date' and end_date='$end_date' 

取得被screen_main_id

select sum(quantity) ,sum(price),sum(%age) from screen_product where screen_main_id='$screen_main_id' group by aitical_id 

它添加,但如果在screen_main_id变化则分为两个部分它不给休息后us sum

如果有任何问题,请帮助我EM了解请我的问题问我 感谢

+0

等待,第一个表中的列的值是什么?你显示四个名字,但只有三列的数据 – dldnh 2012-03-22 15:41:22

+0

我正在使用screen_data当我在start_date和end_date之间获取日期那个时候我正在使用这个字段 – 2012-03-22 18:57:29

回答

0

选择总和(数量),总和(价格),合计(%以下)从screen_product其中 screen_main_id = '$ screen_main_id' 组由aitical_id

在分组,在GROUP BY子句中使用必须在选定字段的字段,以便第二个变成

我希望这会帮助你

+0

它正在为同一个screen_main_id工作,但是当screen_main_id改变时它不会工作的例子时我从表screen_main选择screen_main_id然后假设我有3 screen_main_id是1,2,3然后现在我有3 screen_main_id然后我必须使用artical组所有三个id的总和 – 2012-03-22 18:51:37