Click to See Complete Forum and Search --> : MySQL: Help w/ Query Join?


msmith29063
03-12-2007, 05:37 PM
Sorry, guys. I'm stumped. I've tried various MySQL JOINS to no avail. I need to be able to only return records from one table that have records associated with it in another table.

The existing query is:
SELECT * FROM intranet_dept ORDER BY deptOrder

But, I only want to display records IF the other table references them. The other existing query is:
SELECT * FROM intranet_deptfaq WHERE deptfaqDept=deptID ORDER BY deptfaqOrder

deptID is the ID from the first query. I just want to display records from intranet_dept if there are any records in intranet_deptfaq where deptfaqDept = deptID. Thanks for any help!

Nanscombe
03-12-2007, 05:46 PM
Hi msmith29063,

Do you mean:

SELECT ID.* FROM intranet_dept ID, intranet_deptfaq IDF WHERE IDF.deptfaqDept=ID.deptID ORDER BY ID.deptOrder

Regards
Nigel

msmith29063
03-13-2007, 10:14 AM
Not quite. This as far as I had gotten. It displayed duplicate intranet_dept records -- one for every intranet_deptfaq record.

I have about 8 records in the intranet_dept database table. And about 6 of them are being referenced (by deptID) in the the intranet_deptfaq table (in the deptfaqDept field). I just want to display those 6 records (no duplicates).

Does this make sense? Thank you again!

msmith29063
03-13-2007, 10:20 AM
Got it! I was supposed to be using DISTINCT in my JOIN. Thanks for your help!