SQL question!
| From: | Bruno Gimenes Pereti | Date: | Thu, 27 Jun 2002 13:33:34 +0000 |
| Subject: | SQL question! | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-20157@lists.php.net to get a copy of this message | ||
Hi,
I need a sql query to join multiple lines into only one line. Ok, I wouldn´t
understand this question so I´ll give an example (I´m doing it on mysql but
I need it to mssql). Supose I have 3 tables:
mysql> select * from test1;
+----+-------+
| id | nome |
+----+-------+
| 1 | Bruno |
| 2 | Jorge |
+----+-------+
mysql> select * from test2;
+----+---------+-----------+
| id | request | value |
+----+---------+-----------+
| 1 | 1 | 100,00 |
| 1 | 2 | Mercedes |
| 2 | 1 | 50,00 |
| 2 | 3 | Sparc Sun |
+----+---------+-----------+
mysql> select * from test3;
+----+----------+
| id | value |
+----+----------+
| 1 | money |
| 2 | car |
| 3 | computer |
+----+----------+
test1.id -> test2.id
test3.id -> test2.request
When I execute:
select test1.nome, test3.value, test2.value
from test1, test2, test3
where test1.id = test2.id AND test2.request = test3.id
I get:
+-------+----------+-----------+
| nome | value | value |
+-------+----------+-----------+
| Bruno | money | 100,00 |
| Bruno | car | Mercedes |
| Jorge | money | 50,00 |
| Jorge | computer | Sparc Sun |
+-------+----------+-----------+
But I need this result:
+-------+----------+-----------+------------+
| nome | money | car | computer |
+-------+----------+-----------+------------+
| Bruno | 100,00 | Mercedes | NULL |
| Jorge | 50,00 | NULL | Sparc Sun |
+-------+----------+-----------+------------+
Is it possible in Microsoft SQL Server? I´d like to know if it can be
executed on mysql and, if not, if there is some way to do it in mysql.
Sorry for the big e-mail but it´s something I really need to know.
Thank´s
Bruno Gimenes Pereti.