I have the following SQL query which populates a table with details of various phone calls.
INSERT tblCalls (callerID, destinationNum)
SELECT tblCallerID.callerID, tblSkyNums.skyID
FROM tblCallerID, tblSkyNums
WHERE tblCallerID.callerNumber = '".$caller_id."'
AND tblSkyNums.skyNum = '".$dest_no."'
The query grabs a callerID and a skyID from two seperate tables and inserts them into a third table; tblCalls.
My problem is sometimes no skyID exists for the number entered, and when this happens nothing is entered into tblCalls. Which means no data is recorded for this call.
What I have been trying to do is if no such skyID exists, simply enter ‘n/a’ into the skyID field for that particular call.
I have been at this for over two days now and cant seem to find a way to get it to work.
Any help would be geatly appreciated.
The Coalesce only gets you half way there. The problem is that the ‘,’ in the FROM clause performs an INNER JOIN, and thus if no rows are selected in tblSkyNums, then no rows will be returned from the SELECT query.
To fix this, you need an OUTER JOIN:
Hope this helps!
john…