I have to join two select queries

ashishmishra :

I have to join two select queries and I m using UNION

SELECT 
    DATE(post_orders.created),
    count(post_orders.id) as total_sales_today,
    SUM(IF(user_leads.event_type = 'IN', 1, 0)) as sales_count_inquiry,
    SUM(IF(user_leads.event_type = 'EN', 1, 0)) as sales_count_enroll_now
FROM
    post_orders
    left JOIN 
    user_leads ON post_orders.userid = user_leads.user_id
        AND post_orders.courseid = user_leads.course_id
WHERE
    DATE(post_orders.created) <= '2020-03-03'
    and DATE(post_orders.created) > '2019-10-20'
group by DATE(post_orders.created);


SELECT
  DATE(user_leads.created),
  COUNT(user_leads.id) AS lead_count,
  SUM(IF(users.country = 'IN', 1, 0)) as lead_count_india,
  SUM(IF(users.country = 'US', 1, 0)) as lead_count_usa,
  SUM(IF(users.country <> 'IN' and users.country <> 'US', 1, 0)) as lead_count_row,
  SUM(IF(user_leads.event_type = 'EN' and users.country = 'US', 1, 0)) as lead_count_enrollnow_us
FROM
  user_leads 
  join courses on user_leads.course_id = courses.id
  join users on user_leads.user_id = users.id
WHERE
  DATE(user_leads.created) >= '2019-10-20'
      AND DATE(user_leads.created) < '2020-03-03'
      AND courses.course_type !=4
GROUP BY DATE(user_leads.created) DESC;

But I'm getting this error: The used SELECT statements have a different number of columns 0.238 sec Let me know where I'm wrong and How can I solve it?

Gordon Linoff :

UNION makes no sense in this case. You would end up with two rows per date, one with information about sales and the other with information about leads. And, you have nothing that indicates which row is which.

I'm pretty sure you want an actual JOIN, not a UNION:

SELECT *
FROM (SELECT DATE(po.created) as dte,
             COUNT(*) as total_sales_today,
             SUM(ul.event_type = 'IN') as sales_count_inquiry,
             SUM(ul.event_type = 'EN') as sales_count_enroll_now
      FROM post_orders po LEFT JOIN
           user_leads ul
           ON po.userid = ul.user_id AND
              po.courseid = ul.course_id
      WHERE po.created < '2020-03-04'
            po.created >= '2019-10-21'
      GROUP BY DATE(po.created)
     ) pol LEFT JOIN
     (SELECT DATE(ul.created) as dte,
             COUNT(*) AS lead_count,
             SUM(u.country = 'IN') as lead_count_india,
             SUM(u.country = 'US') as lead_count_usa,
             SUM(u.country <> 'IN' and u.country <> 'US') as lead_count_row,
             SUM(ul.event_type = 'EN' and u.country = 'US') as lead_count_enrollnow_us
      FROM user_leads ul JOIN
           courses c
           ON ul.course_id = c.id JOIN
           users u
           ON ul.user_id = u.id
      WHERE ul.created >= '2019-10-21' AND
            ul.created < '2020-03-04' AND
            c.course_type <> 4
      GROUP BY DATE(ul.created) DESC
     ) l
     USING (dte);

Notes:

  • Table aliases make the query much easier to write and to read.
  • MySQL has a handy shorthand for the SUM(IF()) logic you are using. It treats booleans as numbers, with "1" for true and "0" for false.
  • The use of the function DATE() in a WHERE or ON clause prevents indexes from being used (and can affect other optimizations). I modified the logic to remove that function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23664&siteId=1