Jump to content
php.lv forumi

mysql query


sanchoo

Recommended Posts

ir

table1

field: id

 

table2

field:id

field:a_id

field:b_id

 

ja taisu query:

 

select table1.id from table1 t1, table2 t2 where (t1.id=t2.a_id or t1.id=t2.b_id);

 

tad dabuuju smuki visus t1.id kuri figuree a_id vai b_id, bet kaa lai dabuu tikai tos t1.id kuri NEfiguree ne t2.a_id , ne t2.b_id ?

Link to comment
Share on other sites

Rakstu no galvas, tāpēc varbūt kautkas neies:

SELECT id FROM table1 WHERE id NOT IN (SELECT a_id FROM table2) AND id NOT IN (SELECT b_id FROM table2)

vai arī (ja a_id un b_id nevar būt NULL):

SELECT t1.id 
FROM table1 t1
LEFT JOIN table2 t2a ON t1.id=t2a.a_id
LEFT JOIN table2 t2b ON t1.id=t2b.b_id
WHERE t2a.a_id IS NULL AND t2b.b_id IS NULL

Link to comment
Share on other sites

×
×
  • Create New...