English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

create table mytable(id numder, name varchar(20))
insert into mytable values(1,'john')
insert into mytable values(2,'nati')
insert into mytable values(3,'reda')

How can I select the names john, nati and reda: at once by using a single instraction?

2007-03-25 21:34:03 · 4 answers · asked by Reda Weldebrhan 1 in Computers & Internet Programming & Design

4 answers

select a.name, b.name, c.name
from mytable a,
mytable b,
mytable c
where a.id_number = 1
and b.id_number = 2
and c.id_number = 3

2007-03-25 22:07:09 · answer #1 · answered by AnalProgrammer 7 · 0 0

I know its just an example, but first check if Id and name are reserved words. If they are, then encapsulate them using square brackets.

If the table stays with just the three rows, then

SELECT [name] FROM mytable

should do it. However, if you add more names you may want to do something like

SELECT [name] FROM mytable WHERE [name] IN ('john', 'nati', 'reda')

Obviously if you have more than one john then you may need to change the query to work on the id column instead.

If the names changed, you could use a subquery, and maintain a reference table with all the names you need:

SELECT [name] FROM mytable WHERE [name] IN (SELECT [name] FROM myRefTable)

Remember for large tables IN isn't very efficient.

Hope this helps

2007-03-26 14:43:24 · answer #2 · answered by Anonymous · 0 0

You need a pivot table

Assuming that you mean SQL Server from Microsoft
If you are on SQL2005 look up Pivot Table in Online Help! or if you are on an earlier version goto SQLServerCentral and search for Pivot Table or do the same search on Google.

2007-03-26 08:05:12 · answer #3 · answered by Paul B 3 · 0 0

select name from mytable;

2007-03-26 04:42:00 · answer #4 · answered by Deepali Naik 3 · 0 0

fedest.com, questions and answers