Jump to content
php.lv forumi

atrast tos, kas jau ir izlietoti


hu_ha

Recommended Posts

ir divas tabulas:

produkts:
------------------
prod_id|nos
-------------------
1         |a
2         |b
3         |c
4         |d
5         |e

un pasutijums
------------------
id_pas| id_prod
-------------------
6        | 2
6        | 5
7        | 2
7        | 3

prod_id un id_prod ir tabulu sasaistes lauki.
vaisaajumam buutu jaatgriezh visus produktu nosaukumus, kuri nav tabulaa pasutijums.
piem, ja id_pas=6, tad jaatgriezh: a, c, d
ja id_pas=7, tad : a,d,e

izmeegjinaajos visaadi, bet taa arii neizdevaas. te ir dazhi varianti, kaa meegjinaaju panaakt vajadziigo rezultaatu

SELECT * FROM produkts as a LEFT JOIN 
(select id_prod
from produkts, pasut_prod 
where id_pas=6 and id_prod=prod_id)  as b
ON b.id_prod=a.prod_id WHERE b.id_prod IS NULL;

 

vai arii:

select * from produkts
where prod_id not in 
(select id_prod
from produkts, pasut_prod 
where id_pas=6 and id_prod=prod_id)

un veel paaris varianti, tomeer rezultaata nav.

paldies jau ieprieksh

Link to comment
Share on other sites

Shitais bes subselectiem (mysql <= 4.0x):

 

SELECT produkts.* FROM produkts LEFT JOIN pasutijums ON (produkts.prod_id=pasutijums.id_prod AND pasutijums.id_pas='6') WHERE pasutijums.id_prod IS NULL;

 

Shitaas vareetu buut ar:

 

SELECT * FROM produkts WHERE prod_id NOT IN (SELECT id_prod FROM pasutijums WHERE id_pas='6')

Link to comment
Share on other sites

×
×
  • Create New...