Forum Moderators: open

Message Too Old, No Replies

SQL: some help needed with a query

how to get default when no custom available

         

DoppyNL

3:30 pm on Nov 15, 2005 (gmt 0)

10+ Year Member



So I've got a table with the fields `parent` and `name` (among others, but those don't matter at the moment).

I want to select all records where `parent` is 12.
On top of that I want all the records where `parent` is 1, when the value for `name` doesn't exist where `parent` is 12.

weird huh :)

1 is the default, and I want to `fall back` to those when it is not available in 12.

example:
ID prnt name
A 1 logo
B 1 linkbutton
C 12 logo

For this short list I want to get the ID's B and C (as A already has a parent 12).
And at the end I still need to know what the value of `parent` is...

Does this make sence?

Easy_Coder

4:09 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



like this?

select parent,name
from parents
where parent in(12,1)
and not exists(select parent from parents where name <> '' and parent = 12)

DoppyNL

4:21 pm on Nov 15, 2005 (gmt 0)

10+ Year Member



Haven't tried the code, but I don't think that will do the trick. (also, code below in this post would be faster then that I think).

Background on what I want to do.

The table contains a list of TemplateBits.
So, when we are using the template with id 12, we want all the bits that have `parent` = 12.

But it is possible that that template doesn't customize all bits.
In the example above, Template12 uses the default for the bit called `linkbutton`. The default is Template1, so the parent for the bit is then ofcourse 1.

I could do it by using this query:
select * from TemplateBits where `parent` in (1,12) order by `parent`.
Then walk through the items and store them in an array.
I would then first store all the default bits, and then overwrite them when they are also set in Template12.

But that would mean getting more data from the database, I would rather only get the data that is actually needed.

Easy_Coder

9:30 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



super... sounds like you have it all worked out then

DoppyNL

11:00 am on Nov 16, 2005 (gmt 0)

10+ Year Member



sounds like it yes, as it is 1 way of doing it.

I'd rather fetch less data from the database when possible and making sure that the query doesn't become so complex that it will be more server intensive.

I'm guessing I probably have to use `group by` with the naem and `max` on the id.

Just because it works doesn't mean its the best solution, I'm looking for the best solution :).