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
- SELECT i.*,
- v.`vchLabel` AS ville,
- FROM `stores_info` AS i
- JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille`
- WHERE i.`swActiv`=1 AND (p.idLangue = 'FR' OR p.idLangue IS NULL)
- GROUP BY i.`idStore`;
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
- SELECT i.*,
- v.`vchLabel` AS ville,
- FROM `stores_info` AS i
- JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille`
- JOIN `stores_cp` AS cp ON i.`idStore` = cp.`idStore`
- WHERE i.`swActiv`=1
- AND cp.`cp` LIKE 'H1H1H1'
- GROUP BY i.`idStore`;
Expand|Select|Wrap|Line Numbers
- SELECT i.*,
- v.`vchLabel` AS ville,
- p.`vchLabel` AS province
- FROM `stores_info` AS i
- JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille`
- JOIN `stores_cp` AS cp ON i.`idStore` = cp.`idStore`
- WHERE i.`swActiv`=1
- AND cp.`cp` LIKE 'H1H1H_'
- GROUP BY i.`idStore`;
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
- SELECT i.*,
- v.`vchLabel` AS ville,
- FROM `stores_info` AS i
- JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille`
- WHERE i.`swActiv`=1
- AND '13:20:05' BETWEEN i.`wednesdayopen` AND i.`wednesdayclose`
- AND '18:20:05' < i.`wednesdayclose`
- GROUP BY i.`idStore`;
Expand|Select|Wrap|Line Numbers
- SELECT i.*,
- v.`vchLabel` AS ville,
- FROM `stores_info` AS i
- JOIN `geo_ville` AS v ON i.`idVille` = v.`idVille`
- WHERE i.`swActiv`=1
- AND i.`idVille` IN (401,1102)
- GROUP BY i.`idStore`;
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
- while (no result){
- if (isset(T)){
- unset(T);
- try again;
- }else{
- reset(T);
- }
- if (isset(PC)){
- truncate PC;
- try again;
- }
- }
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.