473,748 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query to narrow results

2 New Member
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.c om 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 2592
bobby44
2 New Member
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
1728
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 selection of digital cameras. The current method I use is to loop though each one of the attributes and their values and run a separate query of each attribute value: for($i=0; $i < attribute_count; $i++) {
2
2416
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 way to only query certain recordset at a time?
29
2477
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" for when and how to use single quotes and double quotes in ASP? thanks! ---------------------- SQL = SQL & "WHERE '" & REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
14
5108
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 DateAdd("d", 6, DateAdd("d", -weekday()+2,)))
2
1980
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 have a query set to use forms!frmMain!txtSearchString as the search criteria for a field. But it is not working. If I go and copy the value of txtSearchString and paste it as cretieria in the query it works but not live and dynamicly pulling it...
10
10151
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 Form that has multilple controls to narrow the results of the query. The functionality I am looking for is to allow a user to enter text into each of the textboxes or into couple or into none. Each textbox corresponds to a field in a Table. ...
1
1149
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 returns results, but if i narrow the results using parametes from the other table, the query does not run, and i get the following error: Method 'Open' of object '_Recordset' failed. Can anyone help me?
4
2622
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 from MS Access to SQL 2005. I get about 689000 rows in SQL Server, vs 863000 rows in MS Access. SELECT T1., T1., T2., MIN ( T1.), MIN(T1. ), T1.COUNT
7
6022
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 a linked table and uses a CDate() expression to convert the timestamp field (Nom_Date) from the table to a date type and the criteria: Between (Now()-183) And Now() to narrow results to the last 6 months. The query works just fine on its own and...
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.