我需要显示2012年的商品名称,商品价格和销售数量。我有这个:
@Entity
@Table(name= "tb_key")
public class KeyEntity {
@Id
private Integer companyid;
private Date creationdate;
private String keyencrypted;
public Integer getCompanyid() {
return companyid;
}
public void setCompanyid(Integer companyid) {
this.companyid = companyid;
}
public Date getCreationdate() {
return creationdate;
}
public void setCreationdate(Date creationdate) {
this.creationdate = creationdate;
}
public String getKeyencrypted() {
return keyencrypted;
}
public void setKeyencrypted(String keyencrypted) {
this.keyencrypted = keyencrypted;
}
@Override
public String toString() {
return "KeyEntity [companyid=" + companyid + ", creationdate=" + creationdate + ", keyencrypted=" + keyencrypted
+ "]";
}
}
这给了我这个
我需要让它看起来像这样:
我该怎么办?
答案 0 :(得分:0)
您在寻找sum()
而不是count()
SELECT m.item_name
, SUM(m.price) as price
, SUM(od.qty) as qty
, DATE_FORMAT(om.odate, '%Y') as date
FROM menu as m
INNER JOIN order_detail od ON od.orderid = m.id
WHERE DATE_FORMAT(om.odate, '%Y') = '2012'
GROUP BY m.item_name, DATE_FORMAT(om.odate, '%Y')
ORDER BY od.qty ASC
LIMIT 7;
答案 1 :(得分:-1)
请检查一下,这将有助于美化您的sql表输出:) Prettifying the select statement output
如果您不希望在输出中显示DATE_FORMAT(om.odate,'%Y')作为日期,则将其从日期中删除。
我相信您的查询必须是:
选择m.item_name,m.price,COUNT(od.qty)作为qty FROM菜单作为m, order_detail为od,order_main为om在哪里m.id = od.orderid和 DATE_FORMAT(om.odate,'%Y')='2012'GROUP BY m.item_name ORDER BY数量 ASC LIMIT 7;