473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Query to narrow results

Hi,
I hope someone can help with a query I'm stuck on.

I think it will involve inner join but I can't get my head around it.

I have two tables that relate to venues fr weddings:
Expand|Select|Wrap|Line Numbers
  1. -- listingDBElements --
  2. ID    field_name    field_value    listing_id    user_id
  3. 23    county          Yorkshire       34          56
  4. 43    county          Yorkshire       26          45
  5. 45    city               London          22           1
  6. 67    venue_name   The Mill         2          22
  7. 55    venue_type      Hall            34           8
  8. 55    venue_type      Hall            22           9
  9. 55    description     Nice place    34           8
  10. 59    venue_type      Hotel           2          55
  11.  
etc..


Expand|Select|Wrap|Line Numbers
  1. -- listingDB --
  2. ID    user_ID    Title                                     active
  3. 1      8             Eastbury Manor House              yes
  4. 2      9             Avenue House                          yes
  5. 3     16            The Red House                            no
  6.  
etc..

I have a search form on the site that people can select or enter information they want to search for on a venue. The fields are as follows:

Venue name (variable = $venue_name)
City or town (variable = $city)
County (variable = $county, a drop down list)
Venue type (variable = $venue_type, a drop down list)

I'd like to return distinct listing ID references from the first table listed above and check with the second table that the listings are marked as 'active=yes'.

But also if someone searches for $venue_types = Halls and also selects $county = Yorkshire I don't want all the venues in Yorkshire and all the Halls in other counties.

Just the Halls in Yorkshire. Same goes for if they search for a town, county, venue type and venue name in one search the result would be only those venues that match all the variables.

Basically its re-writing the directory search form on the home page of weddingvenues.com which passes a url string like:
http://www.weddingvenues.com/listing...enue_type=Barn


So far I have this sql working for just one variable but not sure how to expand to include all 4:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT listingsDBElements.listing_id, listingsDBElements.user_id FROM listingsDBElements, listingsDB WHERE listingsDB.active = 'yes' AND listingsDBElements.field_name = 'venue_type' AND listingsDBElements.field_value like '%barn%' order by listingsDBElements.user_id DESC 
  2.  
Any ideas and I'd be really grateful.

In long hand I guess I'm saying, find all venues where all the field_name values in the first table match the search field names, and all the field_value values match search field values and all have corresponding listing_id's, and finally that these listings are marked as active venues in the second table.

I'm also thinking maybe it's only possible to do using multiple queries and then comparing the results in PHP by looping through each. But I hope to get it working in one sql query.

I am using MySQL client version: 4.1.20 and PHP 4.

Many thanks

Bob
Jan 24 '08 #1
1 2577
Oh, all sorted.
Someone posted this on another site which works a treat. I hope it helps anyone who is in a similar stop
Expand|Select|Wrap|Line Numbers
  1. SELECT vn.listing_id, ld.user_ID, ld.Title
  2.  FROM listingsDB ld
  3. LEFT JOIN listingsDBElements vn
  4.     ON vn.listing_id = ld.ID
  5. LEFT JOIN listingsDBElements vc
  6.     ON vc.listing_id = ld.ID
  7. WHERE ld.active = 'yes'
  8.   AND ( (vn.field_name = 'venue_type' AND vn.field_value like '%$venue_type%')
  9.      OR '$venue_type' = ''
  10.     )
  11.   AND ( (vc.field_name = 'county' AND vc.field_value like '%$county%')
  12.      OR '$county' = ''
  13.     )
  14. order by ld.user_ID DESC
  15.  
cheers
Bob
Jan 24 '08 #2

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

Similar topics

1
by: ben | last post by:
I have been using a nasty combination of php and mysql to generate a narrow down by attribute bar as seen on the likes of shopping.com. For example a user could select 4X Zoom to narrow down a...
2
by: Lin Ma | last post by:
Greetings, In my search application, user can type a number to search. I use LIKE in my query. If a query result generates over 10,000 recordsets, it may several minutes to run. Is there a...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
14
by: deko | last post by:
This runs, but does not narrow to current week. suggestions appreciated! SELECT lngEid, dtmApptDate, Subject, Appt_ID FROM qry002 WHERE (dtmApptDate BETWEEN DateAdd("d",-weekday()+2,) And...
2
by: starke1120 | last post by:
I have a main form with a text box on it "txtSearch" onlost focus it updates another text box "txtSearchString" to include a quote and asterik before and after the value of txtSeach. I then...
10
by: FNA access | last post by:
Hello to the world of the wise, I am a CSOM student at TRU. I am trying to design a database to improve my understanding. The problem I am having is with setting up a query. I have a Query...
1
by: ee97056 | last post by:
Hello all. I have just started to use access and VBA and i have the folowing problem. I have a query that contains an Inner join. I make the query using only parameters from one table and it...
4
by: dsdevonsomer | last post by:
Hello, I have one simple query joining two tables with left outer join on 3 fields and using MIN on two fields. These two tables have lot of data about 3 mil in total. I am trying to migrate db...
7
ollyb303
by: ollyb303 | last post by:
Hi, I am having a bit of a problem with TransferText macro. I am using TransferText, Export Delimited (no field names) to export the results of a query as a .csv file. The query is based on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.