MySQL UNION/JOIN on the same column -
i have normalization table rows 2 foreign keys point primary keys in table of widgets. idea widgets occur in pairs, hence normalization table has 2 columns: widget1 , widget2.
+--------------------+ | widget1 | widget2 | +--------------------+ | 1 | 3 | | 2 | 4 | +--------------------+ each widget has 2 columns well; primary key , name. eg
+------------+ | pk | name | +------------+ | 1 | bob | | 2 | joe | | 3 | max | | 4 | tim | +------------+ i'm trying build query name corresponds other name, i.e.:
+--------------------+ | widget1 | widget2 | +--------------------+ | bob | max | | joe | tim | +--------------------+ unioning 2 select statements gives 1 long column. how can desired output? here's sqlfiddle.
you want 2 joins, not union:
select w1.name, w2.name norm join widgets w1 on w1.pk = norm.widget1 join widgets w2 on w2.pk = norm.widget2 see on sqlfiddle.
Comments
Post a Comment