简体   繁体   中英

MySQL: why inner join works and not outer join in the same syntax

I have a table as below, and I would like to get US and France sales by date. For this scenario, inner join doesn't work.

My question is why inner join works in the query not outer join

query is:

select * from 
(select * from country_sales where country ='US') a
inner join
(select * from country_sales where country ='FR') f
on a.dateid = f.dateid

However on using outer join , I get

Error Code: 1064. You have an error in your SQL syntax; 

output should look like this:

You can USE GROUP BY along with CASE WHEN

SELECT 
dateid,
MAX(CASE WHEN country = 'US' THEN amt ELSE 0 END) us_sales,
MAX(CASE WHEN country = 'FR' THEN amt ELSE 0 END) fr_sales
FROM country_sales
GROUP BY dateid;

Please check the DEMO HERE

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