0

My problem seems simple but I just can't figure it out. I have a single table that I need to construct a two column result from, using the values out of just one column in that table...

ID - ENTITY - CATEGORY
1    Bread    Food
2    Apple    Food
3    Computer Electronics
4    Chair    Furniture
5    TV       Electronics
6    Sofa     Furniture

The results I need should look like this...

EAT_THIS - SIT_HERE
Bread      Chair

Example (wrong) code...

select ENTITY as EAT_THIS from table where CATEGORY = 'Food'
select ENTITY as SIT_HERE from table where CATEGORY = 'Furniture'

.... but I can't seem to figure out how to get these two results on the same line like my example results show. Not sure if this makes sense, please let me know if further detail is needed.

Thanks

fancyPants
  • 50,732
  • 33
  • 89
  • 96
Mike
  • 13
  • 3

2 Answers2

0

Using a join you can achieve this:

SELECT A.entity as EAT_THIS, B.entity as SIT_HERE
FROM tablename A
JOIN tablename B
WHERE A.category = 'Food'
AND B.category = 'Furniture'

However, there is no guarantee that your results will be unique. If anything you'll end up with this:

Bread, Chair
Apple, Chair

...instead of (what I assume you want):

Bread, Chair
Apple, Sofa

Cross joins will help you in this case, see this article: https://stackoverflow.com/a/16471286/2812842

More info

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
0

If you don't specify a relation between the two tables (or in our case the same table), All the possibilities from the Cartesian product will show up

SELECT A.ENTITY AS EAT_THIS , B.ENTITY AS SIT_HERE
FROM tablename A,tablename B
WHERE A.category = 'Food'
AND B.category = 'Furniture'
Uri Goren
  • 13,386
  • 6
  • 58
  • 110