Ask a Question related to Coldfusion Database Access, Design and Development.
-
RuBot #1
Query Problem: Field doesn't always exist
I have 3 tables, a Survey table, a Ticket table, and a Notes Table. The notes
table contains notes for the Ticket, and the Survey is a questionairre about
the Ticket.
Not every Ticket has Notes. I'm creating a site that displays the Surveys, and
related Ticket and Note information. I need it to query the Note table if
applicable.
Let's say I have
<cfquery name = "name" datasource = "db">
select a.*, b.* from ticket a, survey b
where a.ticketid = b.ticketid
</cfquery>
that will only get the surveys and tickets, which will always have matching
fields.
<cfquery name = "name" datasource = "db">
select a.*, b.*, c.*
from ticket a, survey b, notes c
where a.ticketid = b.ticketid and a.ticketid = c.ticketid
</cfquery>
This will only pull records with notes. How do I get records with notes, and
without??
I seem to have this type of query problem often. Any suggestions?:brokenheart;
RuBot Guest
-
Access Denied. The file may not exist, or there could bepermission problem.
I'm seeing few other posts on this subject and am losing my hair so here goes. Using Contribute 3 for my clients to update files while our CMS... -
Calculated Field in Query of Query
How do you add a calculated field to a Query of Query using a function DollarFormat(datafield) as calcField -
Defining field value to exist when not other value
I have a text field definition that I need to make either one value or another. When the record is created, I want it to be value 1, when the user... -
Problem with ADO query against a DBF date field
Marit, Jet and VBScript use the same underlying numeric representations of date/time values, and your best bet is to pass your value as a numeric... -
table does not exist error in AD query?
this runs perfectly as a .vbs but I'd like to convert it to an ASP... any ideas? Response.Buffer = True Dim objConnection, objCommand,... -
mxstu #2
Re: Query Problem: Field doesn't always exist
Use a left join to retrieve all of the records from one table even if there
are no related records in the other. Something like ...
--- not tested. adjust syntax for your database type.
SELECT someColumns
FROM ticket a INNER JOIN survey b ON a.ticketid = b.ticketid
LEFT JOIN notes c ON a.ticketid = c.ticketid
mxstu Guest
-
RuBot #3
Re: Query Problem: Field doesn't always exist
Tables - Survey, Ticket, Notes
Every survey record matches a ticket record, but not all ticket records have a
matching notes record.
Conceptually, isn't the join you listed still looking to match a ticket to a
note? Will this still include records that do not have a matching field?
Sorry I'm a gomer sometimes.
RuBot Guest
-
Kronin555 #4
Re: Query Problem: Field doesn't always exist
What mxstu posted will do exactly what you're asking for.
LEFT JOIN is also sometimes referred to as LEFT OUTER JOIN, but the OUTER is
inferred. Basically, it's like adding a row to the Notes table that matches
anything in the other table that doesn't already have a match in the Notes
table. Oracle accomplished this with the (+) syntax.
Here's some more reading on LEFT JOINs:
[url]http://www.w3schools.com/sql/sql_join.asp[/url]
[url]http://www.devguru.com/Technologies/jetsql/quickref/leftjoin.html[/url]
Kronin555 Guest
-
paross1 #5
Re: Query Problem: Field doesn't always exist
FYI, If you happen to be using a version of Oracle before 9.x, you can not use
the JOIN syntax, but you can use the old proprietary (+) notation in the WHERE
clause for the OUTER join. (Oracle 9 and above, you can use the ANSI-SQL 92
JOIN syntax.)
Something like this:
SELECT someColumns
FROM ticket a, survey b, notes c
WHERE a.ticketid = b.ticketid
AND a.ticketid = c.ticketid(+)
Phil
paross1 Guest
-
RuBot #6
Re: Query Problem: Field doesn't always exist
I have one thing to add: I'm using Microsoft Access to accomplish this task, so I'm thinking the (+) syntax isn't going to work.
Any ideas?
RuBot Guest
-
RuBot #7
Re: Query Problem: Field doesn't always exist
Oh, do what mxstu said right? Let me try that and I'll get back to everyone
RuBot Guest
-
RuBot #8
Re: Query Problem: Field doesn't always exist
Here's my Query, and here's my error:
<cfquery name = "getSurveys" datasource="HelpDesk">
select a.*, b.*, c.*
from survey a inner join Help_Desk_List b
on a.ticketID = b.entry_ID
left join notestbl c
on a.ticketID = c.ticket_ID
</cfquery>
"Syntax error (missing operator) in query expression 'a.ticketID = b.entry_ID
left join notestbl c on a.ticketID = c.ticket_ID'. "
An added bonus to this problem is that I have duplicate ticketIDs in the
survey table '0' which represents random surveys generated unassociated with
any tickets.
I plan on doing this:
<cfquery name = 'getSurvTicketID' datasource = 'HelpDesk'>
select ticketID from survey
</cfquery>
<cfloop query = "getSurvTicketID">
<cfif ticketID neq 0>
<cfquery name = "getSurveys" datasource="HelpDesk">
select a.*, b.*, c.*
from survey a inner join Help_Desk_List b
on a.ticketID = b.entry_ID
left join notestbl c
on a.ticketID = c.ticket_ID
</cfquery>
</cfif>
</cfloop>
RuBot Guest
-
mxstu #9
Re: Query Problem: Field doesn't always exist
>"Syntax error (missing operator)
If you're using Access, I think it requires parenthesis around multiple join
statements. Your best bet is to check your Access documentation. It is
probably something like ....
Select ....
from (survey a inner join Help_Desk_List b
on a.ticketID = b.entry_ID)
left join notestbl c
on a.ticketID = c.ticket_ID
survey>An added bonus to this problem is that I have duplicate ticketIDs in thetickets.>table '0' which represents random surveys generated unassociated with anyWhy loop? If you just want to exclude records where the ticketID value = '0',>I plan on doing this:
then add a where clause to your query...
SELECT ......
FROM survey a .....
WHERE a.ticketID <> '0'
mxstu Guest
-
RuBot #10
Re: Query Problem: Field doesn't always exist
It turns out that I can't try this because........the ticketID field in the
Survey table is actually a string because I use a date/time (mmddyyhhmmss) for
random survey id generation, and it won't save as a long.
Have any ideas for a better random id generator that won't generate duplicates?
RuBot Guest
-
paross1 #11
Re: Query Problem: Field doesn't always exist
Have any ideas for a better random id generator that won't generate duplicates?
With Access, you can use autonumber.
Phil
paross1 Guest
-
RuBot #12
Re: Query Problem: Field doesn't always exist
Yeah, I have the autonumber set as the survey's id, and within each record it
contains the ticketID which is the foreign link to the autonumbered entry_ID in
the help_desk_list table, which in turn links to the autonumbered ticketID in
the notes table, if a record exists.
RuBot Guest
-
RuBot #13
Re: Query Problem: Field doesn't always exist
Well, here's what I did. I changed the field back to numeric and used mmddhhmmss.
RuBot Guest



Reply With Quote

