简体   繁体   English

MYSQL加入问题-需要某种if条件

[英]MYSQL JOIN WHERE ISSUES - need some kind of if condition

Well this will be hard to explain but ill do my best 好吧,这很难解释,但我会尽力而为

The thing is i have 4 tables all with a specific column to relate to eachother. 事情是我有4个表,每个表都有一个与彼此相关的特定列。 1 table with users(agent_users) , 1 with working hours(agent_pers), 1 with sold items(agent_stat),1 with project(agent_pro) the user and the project table is irrelevant in the issue at hand but to give you a better understanding why certain tables is included in my query i decided to still mention them =) The thing is that I use 2 pages to insert data to the working hour and the sold items during that time tables, then i have a third page to summarize everything for current month, the query for that is as following: 1个具有用户(agent_users)的表,1个具有工作时间(agent_pers)的1个,具有售出的项目(agent_stat)的1个,具有项目(agent_pro)的1个用户和项目表与当前问题无关,但可以使您更好地理解为什么我的查询中包含某些表格,我决定仍然提及它们=)问题是,我使用2页将数据插入该时间表内的工作时间和售出商品,然后我有了第三页来汇总所有当月,查询如下:

SELECT *, SUM(sv_p_kom),SUM(sv_p_gick),SUM(sv_p_lunch) FROM ((
agent_users 
LEFT JOIN agent_pers ON agent_users.sv_aid = agent_pers.sv_p_uid) 
LEFT JOIN 
agent_stat ON agent_pers.sv_p_uid = agent_stat.sv_s_uid) 
LEFT JOIN 
agent_pro ON agent_pers.sv_p_pid=agent_pro.p_id
WHERE MONTH(agent_pers.sv_p_datum) =7 GROUP BY sv_aname

so the problem is now that i dont want sold items from previous months to get included in the data received, i know i could solve that by simple adding in the WHERE part MONTH(agent_stat.sv_s_datum) =7 but then if no items been sold that month no data at all will show up not the time or anything. 所以现在的问题是,我不希望以前几个月的已售商品包含在收到的数据中,我知道我可以通过简单地添加WHERE部分MONTH(agent_stat.sv_s_datum)= 7来解决这个问题,但是如果没有售出任何商品该月没有任何数据,不会显示时间或其他任何信息。 Any aid on how i could solve this is greatly appreciated. 非常感谢我如何解决这个问题。 if there's something that's not so clear dont hesitate to ask and ill try my best to answer. 如果有什么不太清楚的地方,请不要犹豫,请尽我所能回答。 after all my english isn't the best out there :P regards breezer 毕竟我的英语不是最好的:P认为微风

您的数据是否可以在WHERE子句中添加(MONTH(agent_stat.sv_s_datum)= 7 OR agent_stat.sv_s_datum IS NULL)?

Fair enough :) -- put the condition as a second condition in your JOIN clause. 足够公平:)-将条件作为第二个条件放在您的JOIN子句中。 ON (agent_pers.sv_p_uid = agent_stat.sv_s_uid AND agent_stat.sv_s_datum = 7) 开启(agent_pers.sv_p_uid = agent_stat.sv_s_uid AND agent_stat.sv_s_datum = 7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM