如何从一个 table 中获取项目列表以及从 mySql 中的花药中获取所有相关项目的列表?
How to get a list of items from one table and total of their related items from anther in mySql?
我要列出所有 order_status
项以及相关订单的总数。我尝试使用 inner join 和 left join,但是 none 对我有用。
select
order_status.order_status_id, order_status.name, sum(order.order_id)
where language_id = 1
http://sqlfiddle.com/#!9/7efcbe
以下是表格:
CREATE TABLE IF NOT EXISTS `order` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`store_id` int(11) NOT NULL DEFAULT '0',
`order_status_id` int(11) NOT NULL DEFAULT '0',
`language_id` int(11) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `order_status` (
`order_status_id` int(11) NOT NULL AUTO_INCREMENT,
`language_id` int(11) NOT NULL,
`name` varchar(32) NOT NULL,
PRIMARY KEY (`order_status_id`,`language_id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
这里是查询,也许你在找这个数据--
select os.order_status_id,os.name,count(o.order_id)
from order_status as os
left outer join `order` o on os.order_status_id = o.order_status_id
group by os.order_status_id,os.name
尝试:
select os.order_status_id, os.name, sum(o.order_id)
from `order_status` os JOIN `order` o on os.order_status_id=o.order_status_id
AND o.language_id = 1
GROUP BY o.order_status_id;
您可以尝试此解决方案来解决您的问题:
select order_status.order_status_id,order_status.name,count(o.order_id) AS no_of_order
from order_status
left outer join `order` o on order_status.order_status_id = o.order_status_id
group by order_status.order_status_id,order_status.name
我要列出所有 order_status
项以及相关订单的总数。我尝试使用 inner join 和 left join,但是 none 对我有用。
select
order_status.order_status_id, order_status.name, sum(order.order_id)
where language_id = 1
http://sqlfiddle.com/#!9/7efcbe
以下是表格:
CREATE TABLE IF NOT EXISTS `order` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`store_id` int(11) NOT NULL DEFAULT '0',
`order_status_id` int(11) NOT NULL DEFAULT '0',
`language_id` int(11) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `order_status` (
`order_status_id` int(11) NOT NULL AUTO_INCREMENT,
`language_id` int(11) NOT NULL,
`name` varchar(32) NOT NULL,
PRIMARY KEY (`order_status_id`,`language_id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
这里是查询,也许你在找这个数据--
select os.order_status_id,os.name,count(o.order_id)
from order_status as os
left outer join `order` o on os.order_status_id = o.order_status_id
group by os.order_status_id,os.name
尝试:
select os.order_status_id, os.name, sum(o.order_id)
from `order_status` os JOIN `order` o on os.order_status_id=o.order_status_id
AND o.language_id = 1
GROUP BY o.order_status_id;
您可以尝试此解决方案来解决您的问题:
select order_status.order_status_id,order_status.name,count(o.order_id) AS no_of_order
from order_status
left outer join `order` o on order_status.order_status_id = o.order_status_id
group by order_status.order_status_id,order_status.name