| | |
sql query string logical problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
Ok, I'm having a big problem with an sql string for a query.
I want to implement a methode that allows the user to search for hotels by entering the minimal price ( so if they enter say 20 it will only display hotels that have prices above 20 ).
SO I setup a textbox to colect the value then I was to create the sql string to make the query. Well at that point everything whent wrong.
Well this is my sql string B4 implementing the minimal price search:
basicly it means I have a few many to 1 conections already in the condition area of the query. the query works perfectly at this point.
Well, the hotel prices are stored in yet another table ( because I have more then one price for a hotel ).
SO the new query would logically be like this:
The link is yet another many to 1 link... linking the unick ID hotel in the base table with a column that identifiew wich price belongs to wich hotel.
at the moment the prices table looks like this:
ID hotel_ID price
1 1 xx
2 1 xy
3 2 yy
meaning thet the first two prices belong to the hotel with the 1 identifyer in the base table
and the 3rd price belongs to the htoel with the 2 identifyer in the basetable.
but addint the extra many to 1 link in the condition area of the string ruins everything...
without specifying any conditions o nthe user's behalf ( meaning it should display ALL the hotels... and there are about 8 of them in the test DB at the moment ) it displays 3 results... namelly it displays the 1 identifyed hotel twice and the 2 identifyed hotel 1 time... and this is DESPITE there beying a DISTINCT select at the beginning of the querry.
i'm sure that this extra condition:
ruins the logical flow of the string... but what can I do about it?
I can't imagine a way in wich to make a separate query to handle the price since the price HAS to be part of the main query... but adding it to the main query ruins the results... can anybody give me an idea about what I coudl do?
I want to implement a methode that allows the user to search for hotels by entering the minimal price ( so if they enter say 20 it will only display hotels that have prices above 20 ).
SO I setup a textbox to colect the value then I was to create the sql string to make the query. Well at that point everything whent wrong.
Well this is my sql string B4 implementing the minimal price search:
PHP Syntax (Toggle Plain Text)
$sql_hstring="SELECT DISTINCT tables.names FROM base table, satelite1, satelite2, satelite3 WHERE ((basetable.city = satelite1.ID ) AND (satelite1.name = satelite2.ID) AND (basetable.something=satelite3.ID)) ORDER BY localitate.den_localitate ASC";
basicly it means I have a few many to 1 conections already in the condition area of the query. the query works perfectly at this point.
Well, the hotel prices are stored in yet another table ( because I have more then one price for a hotel ).
SO the new query would logically be like this:
PHP Syntax (Toggle Plain Text)
$sql_string="SELECT DISTINCT tables.names FROM base table, satelite1, satelite2, satelite3, prices WHERE ((basetable.city = satelite1.ID ) AND (satelite1.name = satelite2.ID) AND (basetable.hotel_id = prices.ID_hotel) AND (basetable.something=satelite3.ID)) ORDER BY localitate.den_localitate ASC";
The link is yet another many to 1 link... linking the unick ID hotel in the base table with a column that identifiew wich price belongs to wich hotel.
at the moment the prices table looks like this:
ID hotel_ID price
1 1 xx
2 1 xy
3 2 yy
meaning thet the first two prices belong to the hotel with the 1 identifyer in the base table
and the 3rd price belongs to the htoel with the 2 identifyer in the basetable.
but addint the extra many to 1 link in the condition area of the string ruins everything...
without specifying any conditions o nthe user's behalf ( meaning it should display ALL the hotels... and there are about 8 of them in the test DB at the moment ) it displays 3 results... namelly it displays the 1 identifyed hotel twice and the 2 identifyed hotel 1 time... and this is DESPITE there beying a DISTINCT select at the beginning of the querry.
i'm sure that this extra condition:
PHP Syntax (Toggle Plain Text)
AND (basetable.hotel_id = prices.ID_hotel)
ruins the logical flow of the string... but what can I do about it?
I can't imagine a way in wich to make a separate query to handle the price since the price HAS to be part of the main query... but adding it to the main query ruins the results... can anybody give me an idea about what I coudl do?
Last edited by marcmm; Dec 7th, 2008 at 11:29 am.
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
•
•
•
•
Use aINNER JOINstatement in your SQL query, placing it beforeWHERE. You may be able to link a common ID field between the two tables and show a result into one set.
at this point I just need to prevent any duplicate posts. To the best of my knowledge DISTINCT is supposed to handle that... but for some reason it backfires in this example and I cannot understand why.
I need it to display results only once.
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: Remov duplicates from array
- Next Thread: number string
| Thread Tools | Search this Thread |
advanced alerts apache api archive array autosuggest beginner binary broken cakephp checkbox class clients cms code cron curl database date datepart display dynamic echo email emptydisplayvalue eregi error execute explodefunction file files folder form forms function functions google hack head href htaccess html if...loop image include insert ip javasciptvalidation javascript joomla keywords library limit link login mail matching menu mlm multiple mysql object oop password paypal pdf php phpincludeissue query radio random recursion recursive remote script search searchbox server sessions shot smarty source space speed sql syntax system table tutorial update upload url validator variable vbulletin video web website youtube





