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?
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:
SUM(IF())
logic you are using. It treats booleans as numbers, with "1" for true and "0" for false.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.Yau have this:
select t1.1
, t1.2
, t1.3
, t1.4
from mytable t1
union
select t2.1
, t2.2
, t2.3
, t2.4
, t2.5
, t2.5
from mytable t2
and you need this for union to be a success:
select t1.1
, t1.2
, t1.3
, t1.4
from mytable t1
union
select t2.1
, t2.2
, t2.3
, t2.4
from mytable2 t2
The problem is that there is a different number of columns in your select clause of your first select than in your second select.
Also, lets say the first select gives this results:
| t1.1 | t1.2 | t1.3 | t1.4 |
| 1 | 2 | 3 | 4 |
And lets imagine the second one gives this result:
| t2.1 | t2.2 | t2.3 | t2.4 |
| 5 | 6 | 7 | 8 |
Union will give you this results:
| t1.1 | t1.2 | t1.3 | t1.4 |
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
And one of the joins will give you this result:
| t1.1 | t1.2 | t1.3 | t1.4 | t2.1 | t2.2 | t2.3 | t2.4 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Hope this helps. Cheers!
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.