views:

55

answers:

1

I am working backwards with a query. I have a select query that fetches some rows...but now I am working backwards and want to insert entries in concerned tables to that the select query brings back the stuff I inserted.

Select Query:

SELECT DISTINCT a.catalogID, a.topicID, a.topicShortName
FROM catalog_lu_topics a
LEFT JOIN catalog b ON a.catalogID = b.catalogID
LEFT JOIN catalog_topics d ON ( a.topicID = d.topicID
AND d.topicID != 'top295' )
LEFT JOIN catalog_topics e ON ( (
d.catalogID = e.catalogID
)
AND (
e.topicID != d.topicID
) )
LEFT JOIN catalog_lu_topics f ON f.topicID = e.topicID
WHERE a.displayStatus != 'hide'
AND f.parentID = 'top305'

I've inserted entries in the following tables:

catalog
catalog_lu_topics (made sure that parentID = 'top305')
catalog_topics (made sure topicid is not top295)
catalog_topics (additional entry, with different topicID)

After the insert statements if I run the above query again, i do not see the new entries. But If I remove the following entries from the above select query then I see the new entries:

AND (
    e.topicID != d.topicID
    )

I cant wrap my head around this and need some advice.

EDIT: result of select a.* e.topicid, d.topicid (after adding new entries). Note new entries do not show up in this resultset

top335      114   Victorian Grace   top301   5   gvl107   Desc   display   2007-03-26 12:38:46   top318   top335
top329  34  Revived Georgian  top301  2  gvl107  Desc  display  2007-03-26 12:38:46  top318  top329
top338  117  DC Townhouse  top301  4  gvl107  Desc  display  2007-03-26 12:38:47  top318  top338
top329  34  Revived Georgian  top301  2  gvl107  Desc  display  2007-03-26 12:38:46  top316  top329
top336  115  Bespoke in Bethesda  top301  6  gvl107  Desc  display  2007-03-26 12:38:46  top316  top336
+1  A: 

Okay, here's what's happening.

When you have that condition in there, you're getting nulls in your query for all of e, which is subsequently used to pull back f, which you then filter on. Ergo, you're doing an inner join but going about it in a roundabout way.

The update:

So, the entries added contained:

a.catalogid = 297
a.topicid = top398

d.catalogid = 298
d.topicid = top398

e.catalogid = 299
e.topicid = top400

So, when a joins to d, it goes via the topicid, and lo and behold, finds something. Then d takes that catalogid (298), and matches it up to e. Except, e doesn't find anything, so returns null. Then f tries to join with e.topicid. Sadly, e.topicid is null, but there's an f.parentid value in the where clause. Therefore, this row gets filtered out.

Eric
I just recently added: 1) catalogID = '297' to catalog table. 2) topicID = 'top398' into catalog_lu_topics. 3) topicID = 'top399' into catalog_lu_topics again. 4) catalogid = '298' into catalog_topics 5) catalogid = '299' into catalog_topics again.
josh
That returns just one topic (398), and so fails that `e.topicid != d.topicid` condition, making its `f.parentid null`, making it go away in the query.
Eric
Ah, see, `e` and `d` are matching on the `d.catalogid`, and the only `d.catalogid` with a `topicid` of `top398`, the `a.topicid`, is `298`. Therefore, since there's only one `topic` row that meets these criteria, it gets filtered out with the `e.topicid != d.topicid`.
Eric
so where do you think i need to add more so that and e.topicID != d.topicID condition passes and does not return nulls
josh
I know I'm so close! >_<
josh
Add a record in `catalog_topics` that is `catalogid: 298` and `topicid: top399`. That should do it!
Eric
Or do `update catalog_topics set catalogid = 298 where topicid = 'top400'`
Eric
thanks so much!
josh
Not a problem, glad I could help!
Eric