Show the agent name even not found?

时间:2015-11-12 11:17:26

标签: mysql

Is there a way to show agent_call in the result to include everything from agent_call IN ('Agent1', 'Agent2', 'Agent3', 'Agent4')

For example, the result only found agent_call Agent2 and Agent4 with relevant data and other none matching agent should be null.

SELECT monitoring_session.id, session_date, agent_call, Count(*)   AS total 
FROM   `monitoring_session` 
        JOIN monitoring_campaign  ON monitoring_campaign.id = monitoring_session.monitoring_campaign_id 
WHERE  agent_call IN ('Agent1', 'Agent2', 'Agent3', 'Agent4') 
GROUP  BY Date(session_date), agent_call

3 个答案:

答案 0 :(得分:1)

Try this query:

SELECT 
    monitoring_session.id, 
    session_date, 
    IF(agent_call IN ('Agent1', 'Agent2', 'Agent3', 'Agent4'), agent_call, NULL) AS agent_call, 
    Count(*)   AS total 
FROM   `monitoring_session` 
LEFT JOIN monitoring_campaign  ON monitoring_campaign.id = monitoring_session.monitoring_campaign_id
GROUP  BY Date(session_date), agent_call

答案 1 :(得分:1)

You need to use a LEFT JOIN if you want to get a result even if the join does not return anything.

Try this:

SELECT monitoring_session.id, session_date, agent_call, Count(*)   AS total 
FROM   `monitoring_session` 
        LEFT JOIN monitoring_campaign  ON monitoring_campaign.id = monitoring_session.monitoring_campaign_id 
WHERE  agent_call IN ('Agent1', 'Agent2', 'Agent3', 'Agent4') 
GROUP  BY Date(session_date), agent_call

答案 2 :(得分:0)

try this............

SELECT monitoring_session.id, session_date, agent_call , Count(*)   AS total 
FROM   `monitoring_session` 
LEFT JOIN monitoring_campaign  ON (monitoring_campaign.id = monitoring_session.monitoring_campaign_id AND agent_call IN ('Agent1', 'Agent2', 'Agent3', 'Agent4'))  
GROUP  BY Date(session_date), agent_call