472,123 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,123 software developers and data experts.

Advance joined query, need help

Hi all,

I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in the whole. I really need MySQL wizz to give me a hand (and maybe refer me to books to get me to the wizz level myself).

First off, english is a second language to me and sometimes my sentences might be a little awkward. Please forgive me. Mon français est bien meilleur.

Description of database
My database is pretty large and I tried to design it following the best practice (using a lot of many-to-many relationship tables). It holds the description of over 200 stores, with opening hours, services, locations, etc. I am trying to create a AJAX store search engin, with Google Map display, where the user can choose from a number of fields the conditions of his search. When he validates his search, the query is sent to the server, which retrieves the list and update the map.

The conditions are:
  • Postal Code
  • City id
  • Service id
  • Opening hours

Next is the schematic description of the table store_info
| idStore | ... | idCity | openingHourMonday | closingHourMonday | openingHourTuesday | closingHourTuesday | ... |

The schematic description of the table store_service
| idService | vchLabel | ... |

The schematic description of the table store_info_service
| idService | idStore |

The schematic description of the table store_info_postalcode
| vchPostalCode | idStore |

(NOTE: those Postal Codes are based on the flyer distribution list, so it doesn't contain all the postal code of the country, and some user input can return NULL).


the Script
Getting the information out with JOINED query is not a problem, the trouble is getting the right data out. Getting the conditions expressions right is what I need.

So, I've built my PHP script to compose my query in parts ($SELECT, $FROM, $WHERE). Based on the GET parameters, I add to the query string the needed expressions to refine the query. I unite the parts in $query, and run it. Its been working OK, but not up to my linking. I wan't it bullet proof.

Here is the base query:

Expand|Select|Wrap|Line Numbers
  1. SELECT i.*, 
  2. v.`vchLabel` AS ville, 
  3. FROM `stores_info` AS i 
  4. JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille` 
  5. WHERE i.`swActiv`=1 AND (p.idLangue = 'FR' OR p.idLangue IS NULL) 
  6. GROUP BY i.`idStore`;
Here are some of my solutions:

1. Postal code: If a user input a postal code which returns no data, needs to be repeated with a substring of the P.C. (eg.H1H1H1 -> H1H1H_). Using LIKE in

Expand|Select|Wrap|Line Numbers
  1. SELECT i.*, 
  2. v.`vchLabel` AS ville, 
  3. FROM `stores_info` AS i 
  4. JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille` 
  5. JOIN `stores_cp` AS cp ON i.`idStore` = cp.`idStore` 
  6. WHERE i.`swActiv`=1
  7. AND cp.`cp` LIKE 'H1H1H1' 
  8. GROUP BY i.`idStore`;
return 0

Expand|Select|Wrap|Line Numbers
  1. SELECT i.*, 
  2. v.`vchLabel` AS ville, 
  3. p.`vchLabel` AS province 
  4. FROM `stores_info` AS i 
  5. JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille` 
  6. JOIN `stores_cp` AS cp ON i.`idStore` = cp.`idStore` 
  7. WHERE i.`swActiv`=1
  8. AND cp.`cp` LIKE 'H1H1H_' 
  9. GROUP BY i.`idStore`;
return 1 store


2. Closing time: the user can ask for stores open now and for the next 30, 60, or 300 minutes (5hre). It could be any number of minutes, but if it goes later then midnight, the closing time is removed and the user receives a warning.

Expand|Select|Wrap|Line Numbers
  1. SELECT i.*, 
  2. v.`vchLabel` AS ville, 
  3. FROM `stores_info` AS i 
  4. JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille` 
  5. WHERE i.`swActiv`=1
  6. AND '13:20:05' BETWEEN i.`wednesdayopen` AND i.`wednesdayclose` 
  7. AND '18:20:05' < i.`wednesdayclose` 
  8. GROUP BY i.`idStore`;
3. idCity: the user can choose multiple cities from a drop-down list. The id get passed to the request in the form of a list separated by coma. I then use the IN() statement

Expand|Select|Wrap|Line Numbers
  1. SELECT i.*, 
  2. v.`vchLabel` AS ville, 
  3. FROM `stores_info` AS i 
  4. JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille` 
  5. WHERE i.`swActiv`=1
  6. AND i.`idVille` IN (401,1102) 
  7. GROUP BY i.`idStore`;
Returns the list of all the stores located in city 401 or 1102.

And thats where it gets complicated.

Trouble #1: The services
The user has checkbox associated with the idService. The idServices get sent to the request in the same form as the idCity (eg. 1,4).

At first I used the IN() statement, but realised that it got the store that offers service 1 OR 4. I needed the stores that offered the two services.

So I used = to compare the string to a subquery (SELECT GROUP_CONCAT(idServices) FROM store_info_service WHERE idStore = $idStore), but of course a store that offers services 1,2,3,4 was not returned, but it DID offer the services requested. I tried the IN() construct with this subquery, but nothing conclusive came out.

Trouble #2: Concurrent search clauses
The user can select more then one condition for his search. For exemple, he could look for the stores open in the next hour, around his own postal code (eg. H1H___).

It has been a requisite of this project that the user receives listing whatever the cost. If one condition is not met, it is bypassed to give the stores corresponding to the other conditions. A javascript alert informs the user of that.

How do I structure such a script? I've made it work with TIME and POSTAL CODE, doing repeated queries until a result is found.

Expand|Select|Wrap|Line Numbers
  1. while (no result){
  2.      if (isset(T)){
  3.           unset(T);
  4.           try again;
  5.      }else{
  6.           reset(T);
  7.      }
  8.  
  9.      if (isset(PC)){
  10.           truncate PC;
  11.           try again;
  12.      }
  13. }
  14.  
Of course, this is a simplified version of my PHP script. That way, I try with the T, without the T, reducing PC every 2 queries. It seems pretty hard and time consumming right now, imagine when I add CITY and SERVICE to the equations.

Does anyone have an idea how I could simplify this process?

One hypothesis was to run the 4 queries separately and cross-referenced the results in PHP, only keeping the result sets which have common results, and keeping a warning on the defective queries. If a query is really messed up, I could keep only one condition and return these results, based on semantic priority (Postal code > City > Opening hours > Services).

As you can see this is a though one. I'd really like to be able to wrap my mind around problems of the kind and come up with effective solutions. I tried to find books only about queries (not administration). If you have any recommandation, go ahead.

Thanks.
Dec 20 '06 #1
0 2336

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Mike Leahy | last post: by
1 post views Thread by Michael | last post: by
2 posts views Thread by GGerard | last post: by
1 post views Thread by Ersin Gençtürk | last post: by
1 post views Thread by Ike | last post: by
5 posts views Thread by zMisc | last post: by
4 posts views Thread by Frank List | last post: by
kiss07
4 posts views Thread by kiss07 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.