简体   繁体   中英

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. 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:

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. 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

您的数据是否可以在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. ON (agent_pers.sv_p_uid = agent_stat.sv_s_uid AND agent_stat.sv_s_datum = 7)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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