views:

26

answers:

1

Hi again, using mysql and php,

I have two tables, I'm french so the table names may sound weird, but I would like to keep them as originals to avoid errors when testing…

"arborescence" (it means categories)
"arborescence_domaine" (it means the subdomains for the categories)

The aim of this system is to be able to have great control
on the subdomains that depends on two things : the category and the zipcode.
Just to explain you before I dive into the problem,
for the same category, let's say "car",
for the zipcode 37000 the subdomain will be "supercars"
but for the zipcode 93000 the subdomain for the same category will be for example "the93supercars"

That's why the "arborescence_domaine" contains the following fields :
id - arborescence_id - DEP - domaine - statut

where "DEP" is the zipcode and "domaine" the subdomain and "statut" is a bool that decides if the subdomain is activated or not.

Now, the fun part begins …

What I would like to do is do it within 1 single sql query if possible. (otherwise, I know thousands of php methods to achieve the same goal).

What I want now is get all the subdomains from a given array of category and a given zipcode. If the subdomain is not set, it doesn't exist in the "arborescence_domaine" table.

But I want to have a result like this one :

cat_id domaine DEP
3 vehicule-occasion 37
14 immobilier 37
20 www NULL
26 services-divers 37
28 www NULL
37 www NULL
43 www NULL
3467 www NULL

That means that if the subdomain is not set, it may returns the default 'www' value.

Now without the zipcode I learned how to do it a faw hours ago :

This is a working request (the one that displayed the above array)
SELECT a.id as cat_id, IFNULL(d.domaine, 'www') as domaine, d.DEP
FROM arborescence a
LEFT JOIN arborescence_domaine d on d.arborescence_id=a.id
WHERE a.id in(3,14,20,26,28,37,3467,43);

But now with the zipcode I don't get the expected result :

The following request :

SELECT a.id as cat_id, IFNULL(d.domaine, 'www') as domaine, d.DEP
FROM arborescence a
LEFT JOIN arborescence_domaine d on d.arborescence_id=a.id
WHERE a.id in(3,14,20,26,28,37,3467,43) AND d.DEP='37';

Give the following result :

cat_id domaine DEP
3 vehicule-occasion 37
14 immobilier 37
26 services-divers 37

Well, the "in database not existing subdomains" have gone away.

Now the question is : Is it possible to have a result like the first result shown, but using a zipcode filtering ?

(I thought I got it, but then a new problem occurred... as always...)

A: 

The d.DEP should be in your ON clause:

SELECT a.id as cat_id, IFNULL(d.domaine, 'www') as domaine, d.DEP
FROM arborescence a
LEFT JOIN arborescence_domaine d 
    ON d.arborescence_id=a.id AND d.DEP='37'AND d.status
WHERE a.id in(3,14,20,26,28,37,3467,43);
Wrikken
Yes!I didn't know that one could combine the on clause within a left join.Your answer is the one I was looking for.Now my app is gonna rock !Master, thank you for sharing your knowledge,I feel really more powerfull now ;)
ling