DaveThomasPilot
asked on
SQLite Query needed
I need to do a select from a table doing something like this:
select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column1
Basically, the values in column1 of the MANYCOLUMNS table are used to find a name from the IDTONAME table.
This would work fine, I only wanted a single column in the result set, but I need several columns, each with the id in the M table used to find the name from the n table.
I tried this:
select (select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column1),
(select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column2),
.....
but then even the first column is not correct---I get the same name for each record in the result set instead of the name associated with the id in the column.
Thanks,
Dave Thomas
select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column1
Basically, the values in column1 of the MANYCOLUMNS table are used to find a name from the IDTONAME table.
This would work fine, I only wanted a single column in the result set, but I need several columns, each with the id in the M table used to find the name from the n table.
I tried this:
select (select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column1),
(select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column2),
.....
but then even the first column is not correct---I get the same name for each record in the result set instead of the name associated with the id in the column.
Thanks,
Dave Thomas
You need to use UNION
If you need to sort it as well, then add ID to the selection and sort by it
select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column1
UNION ALL
select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column2
UNION ALL
select n.name from IDTONAME n, MANYCOLUMNS M where n.id=M.column3
etc...
If you need to sort it as well, then add ID to the selection and sort by it
select n.name, n.ID from IDTONAME n, MANYCOLUMNS M where n.id=M.column1
UNION ALL
select n.name, n.ID from IDTONAME n, MANYCOLUMNS M where n.id=M.column2
UNION ALL
select n.name, n.ID from IDTONAME n, MANYCOLUMNS M where n.id=M.column3
ORDER BY 2
ASKER
That gives me the data, but two many rows--each union will have the same number of records as MANYCOLUMNS----not what I want.
ASKER
I finally found something that I think works...
SELECT
C1.name,
C2.name,
C3.name,
.....
FROM MANY_COLUMNS
LEFT_OUTER_JOIN IDTONAME AS C1
ON MANYCOLUMNS.COLUMN1=C1.NAM E
LEFT_OUTER_JOIN IDTONAME AS C2
ON MANYCOLUMNS.COLUMN2=C2.NAM E
LEFT_OUTER_JOIN IDTONAME AS C3
ON MANYCOLUMNS.COLUMN3=C3.NAM E
...
Maybe there's an easier way, but it seems to work.
SELECT
C1.name,
C2.name,
C3.name,
.....
FROM MANY_COLUMNS
LEFT_OUTER_JOIN IDTONAME AS C1
ON MANYCOLUMNS.COLUMN1=C1.NAM
LEFT_OUTER_JOIN IDTONAME AS C2
ON MANYCOLUMNS.COLUMN2=C2.NAM
LEFT_OUTER_JOIN IDTONAME AS C3
ON MANYCOLUMNS.COLUMN3=C3.NAM
...
Maybe there's an easier way, but it seems to work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Is that different than what I posted, or did you post pretty much the same time I did?
I have not seen your answer when I was typing mine. Anyway, as you have found the answer yourself, you can request to close the question, or accept your answer as a solution
>>Maybe there's an easier way
nope: no easier way.
nope: no easier way.
ASKER