Skip to content

Instantly share code, notes, and snippets.

@btedev
Created November 27, 2012 21:43
Show Gist options
  • Save btedev/4157285 to your computer and use it in GitHub Desktop.
Save btedev/4157285 to your computer and use it in GitHub Desktop.
MySQL temp table example for missing values
create table pantry(name varchar(50), quantity int);
insert into pantry values('soup', 10), ('celery', 5), ('crackers', 2);
create temporary table shopping_list(name varchar(50));
insert into shopping_list values ('soup'), ('onion'), ('celery');
select a.name, ifnull(b.quantity, 0) as quantity
from shopping_list a
left join pantry b on a.name = b.name;
+--------+----------+
| name | quantity |
+--------+----------+
| soup | 10 |
| onion | 0 |
| celery | 5 |
+--------+----------+
3 rows in set (0.00 sec)
drop table shopping_list;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment