Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

SQL select query

Dear expert
Please help with a select query
i got a multiple select query like:
select * from data.test where id = 1245
select * from data.test2 where id = 1245
select * from data.test5 where id = 1245
I would like them to be in a column instead of multiple results windows shows up.
i tried:
Select (
select * from data.test where id = 1245
select * from data.test2 where id = 1245
)
Didnt work that way. Thx.
Avatar of Norie
Norie

You could use UNION.

SELECT * FROM DATA.TEST WHERE ID = 1245
UNION

SELECT * FROM DATA.TEST2 WHERE ID = 1245
UNION

SELECT * FROM DATA.TEST5 WHERE ID = 1245
Avatar of WeTi

ASKER

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Is that an error message you are receiving?

If it is it means that the tables don't have the same number of fields.

Which fields, that are common to all the tables, do you want to return?
Avatar of WeTi

ASKER

I think the union only works when the result is one row (Or column?) some of the select returns with multiple rows.
I think the union only works when the result is one row (Or column?) some of the select returns with multiple rows.
Union only works with the same number and type of columns. The number of rows is unimportant. If two rows are the same, only one will be returned so if you want all rows, you can use UNION ALL.

If you want them to be on a single row, you need to either use some form of pivot or construct the columns of the result set by joining the different tables.
Avatar of WeTi

ASKER

I only want them to show in a single table.
ASKER CERTIFIED SOLUTION
Avatar of Martyn Spencer
Martyn Spencer
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi

ASKER

Thanks for the help, but why cant you use * ? I don't really care about what columns to show up tho.
You could use * in my above example. You will just see column data that is potentially irrelevant. For example, why show each test table's ID field, since they will always be the same and will always be the value that you supply as a parameter?