现在有两个表,一个商品表,字段如下:
CREATE TABLE ft_products
(p_id
int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品id',p_name
varchar(50) NOT NULL DEFAULT '' COMMENT '商品名字',hot
tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否热门: 1是 0否',sort
tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '排序',status
tinyint(1) NOT NULL DEFAULT '1' COMMENT '1上架2下架3删除',
PRIMARY KEY (p_id
)
)
一个商品标准表,字段如下:
CREATE TABLE ft_products_standard
(p_s_id
int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品规则表',p_id
int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商品id',price
decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '单价',buyed_count
int(5) unsigned NOT NULL DEFAULT '0' COMMENT '已筹份数',total_count
int(5) unsigned NOT NULL DEFAULT '0' COMMENT '总需份数',c_time
int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
PRIMARY KEY (p_s_id
)
)
现在要根据这两个表,是一个商品对应多个价格的。我要根据已筹份数来进行排序。但是分页又要按照商品来。请问sql语句应该怎么写?
select p_name,hot,sort,status,sum(buyed_count) as total_buyed_count from ft_products_standard left join ft_products on ft_products_standard.p_id=ft_products.p_id group by ft_products_standard.p_id order by total_buyed_count desc limit 0,10