alex_user 0 Newbie Poster

Hello,

I need to select data from 3 table simultaneously in one query and i can't get iti work...please help if you can. Thanks in advance!
my tables:

mysql> describe produse_ofertate;
+-------------+-----------------+------+-----+---------+----------------+
| Field       | Type            | Null | Key | Default | Extra          |
+-------------+-----------------+------+-----+---------+----------------+
| id          | int(6) unsigned | NO   | PRI | NULL    | auto_increment | 
| id_oferta   | int(11)         | NO   |     | NULL    |                | 
| nume_produs | varchar(300)    | NO   |     | NULL    |                | 
+-------------+-----------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


mysql> describe produse_cantitate;
+-----------+-----------------+------+-----+---------+----------------+
| Field     | Type            | Null | Key | Default | Extra          |
+-----------+-----------------+------+-----+---------+----------------+
| id        | int(6) unsigned | NO   | PRI | NULL    | auto_increment | 
| id_oferta | int(11)         | NO   |     | NULL    |                | 
| cantitate | int(11)         | NO   |     | 0       |                | 
+-----------+-----------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> describe produse_pret;
+-----------+-----------------+------+-----+---------+----------------+
| Field     | Type            | Null | Key | Default | Extra          |
+-----------+-----------------+------+-----+---------+----------------+
| id        | int(6) unsigned | NO   | PRI | NULL    | auto_increment | 
| id_oferta | int(11)         | NO   |     | NULL    |                | 
| pret      | decimal(8,2)    | NO   |     | NULL    |                | 
+-----------+-----------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

This is what I have in tables:

mysql> SELECT produse_ofertate.nume_produs FROM produse_ofertate WHERE produse_ofertate.id_oferta = 4;
+-------------+
| nume_produs |
+-------------+
| aaaaa       | 
| dsaddas     | 
| asasa       | 
| dddd        | 
+-------------+
4 rows in set (0.00 sec)

mysql> SELECT produse_cantitate.cantitate FROM produse_cantitate WHERE produse_cantitate.id_oferta = 4;
+-----------+
| cantitate |
+-----------+
|         2 | 
|         1 | 
|         1 | 
|         1 | 
+-----------+
4 rows in set (0.00 sec)

 
mysql> SELECT produse_pret.pret FROM produse_pret WHERE produse_pret.id_oferta = 4 ;
+-------+
| pret  |
+-------+
| 80.00 | 
| 76.00 | 
| 54.00 | 
| 36.00 | 
+-------+
4 rows in set (0.00 sec)

And this is the best result I could get, but it only returns 2 values for "cantitate" (there are 4), and all data is on a single columns and it should be on separate columns...

mysql> (SELECT produse_ofertate.nume_produs FROM produse_ofertate WHERE produse_ofertate.id_oferta = 4) UNION (SELECT produse_cantitate.cantitate FROM produse_cantitate WHERE produse_cantitate.id_oferta = 4) UNION (SELECT produse_pret.pret FROM produse_pret WHERE produse_pret.id_oferta = 4);
+-------------+
| nume_produs |
+-------------+
| aaaaa       | 
| dsaddas     | 
| asasa       | 
| dddd        | 
| 2           | 
| 1           | 
| 80.00       | 
| 76.00       | 
| 54.00       | 
| 36.00       | 
+-------------+
10 rows in set (0.01 sec)

I need 3 columns: nume_produs, cantitate and pret, in a single table. If you can help me, please do. Thanks!

Alex

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.