[英]How to join a table to this SQL code?
I am using this awesome code from @Richard aka cyberkiwi to run a query (it returns the sum of value
for each month, for each plant): 我正在使用@Richard又名cyberkiwi的出色代码来运行查询(它返回每个工厂每个月的
value
总和):
table name: data 表名:数据
record_id id_fk plant_id_fk date value category_1
1 1 1 2011-03-01 10 A
2 1 1 2011-03-02 10 A
3 1 1 2011-04-10 5 B
4 1 2 2011-04-15 5 C
SQL code SQL代码
select up.id_fk, up.plant_id_fk, ym2, ifnull(sum(data.value_1),0) totalvalue_1
from (select distinct date_format(date, '%Y-%m') ym, date_format(date, '%M %Y') ym2 from data) dates
cross join (select distinct data.id_fk, data.plant_id_fk from data) up
left join data on date_format(data.date, '%Y-%m') = dates.ym
and up.id_fk=data.id_fk
and up.plant_id_fk=data.plant_id_fk
and category_1='A'
group by up.id_fk, up.plant_id_fk, ym2, ym
order by up.id_fk, up.plant_id_fk, date(concat(ym,'-1'))
Now I need to join this with the following table in order to run a PHP loop that will retrieve the plant_name
instead of the plant_id
. 现在,我需要将其与下表连接,以便运行一个PHP循环,该循环将检索
plant_name
而不是plant_id
。
table name: plants 表名:植物
id_fk plant_id plant_name
1 1 oak tree
1 2 cherry tree
Anyone know where to insert the clause that would create this join? 有人知道在哪里插入将创建此联接的子句吗?
Thanks! 谢谢!
Modified as shown below 修改如下图
select
选择
up.id_fk,up.id_fk,
p.plant_name,p.plant_name,
ym2,YM2,
ifnull(sum(data.value_1),0) totalvalue_1ifnull(sum(data.value_1),0)totalvalue_1
from (来自(
select distinct date_format(date, '%Y-%m') ym, date_format(date, '%M %Y') ym2选择不同的date_format(date,'%Y-%m')ym,date_format(date,'%M%Y')ym2
from data) dates从数据)日期
cross join (交叉连接(
select distinct data.id_fk, data.plant_id_fk选择不同的data.id_fk,data.plant_id_fk
from data) up从数据)
inner join plants p on p.plant_id = up.plant_id_fkp.plant_id = up.plant_id_fk上的内部连接植物p
left join data左联接数据
on date_format(data.date, '%Y-%m') = dates.ym在date_format(data.date,'%Y-%m')= dates.ym
and up.id_fk=data.id_fk和up.id_fk = data.id_fk
and up.plant_id_fk=data.plant_id_fk以及up.plant_id_fk = data.plant_id_fk
and category_1='A'和category_1 ='A'
group by up.id_fk, up.plant_id_fk, ym2, ym, p.plant_name按up.id_fk,up.plant_id_fk,ym2,ym, p.plant_name分组
order by up.id_fk, up.plant_id_fk, date(concat(ym,'-1'))按up.id_fk,up.plant_id_fk,date(concat(ym,'-1'))排序
Hm. 嗯。 I don't think the recordset and the query are matching;
我认为记录集和查询不匹配; I don't see the field record_id from the fieldset.
我没有从字段集中看到字段record_id。 I will adjust the query you provided.
我将调整您提供的查询。
select up.id_fk, up.plant_id_fk, pn.plant_name, ym2, ifnull(sum(data.value_1),0) totalvalue_1
from (select distinct date_format(date, '%Y-%m') ym, date_format(date, '%M %Y') ym2 from data) dates
cross join (select distinct data.id_fk, data.plant_id_fk from data) up
left join data on date_format(data.date, '%Y-%m') = dates.ym
and up.id_fk=data.id_fk
and up.plant_id_fk=data.plant_id_fk
and category_1='A'
INNER JOIN plants pn ON up.plant_id_fk = pn.plant_id
group by up.id_fk, up.plant_id_fk, ym2, ym
order by up.id_fk, up.plant_id_fk, date(concat(ym,'-1'))
This should work. 这应该工作。 I added the pn.plant_name to the SELECT area, and used a INNER JOIN to join the plants table to the up selection for the field plant_id_fk.
我将pn.plant_name添加到SELECT区域,并使用INNER JOIN将plants表联接到field_id_fk字段的向上选择中。
As an aside, it makes it a lot easier to read if the SQL elements are capitalized (SELECT, JOIN, GROUP etc) as well as your table definitions (UP, YM2, YM, DATES etc etc). 顺便说一句,如果将SQL元素大写(SELECT,JOIN,GROUP等)以及表定义(UP,YM2,YM,DATES等),则使阅读起来容易得多。 This is just my personal preference.
这只是我的个人喜好。
Nishan Karassik www.medfocusrcm.com 尼山·卡拉西克(Nishan Karassik)www.medfocusrcm.com
Just before the group by 就在该小组之前
left join plants on data.id_fk=plants.id_fk and data.plant_id_fk=plants.plant_id
and then replace in the group by and select {up.id_fk, up.plant_id_fk} by plant.plant_name 然后替换为组,并按plant.plant_name选择{up.id_fk,up.plant_id_fk}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.