473,394 Members | 1,781 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,394 software developers and data experts.

How to get intersecting results from multiple queries (Joins)?

1
Hello.

I'm programming a website in which we have sport clubs listed in a DB, structure is as follows:

club (id_club, club_name, club_city)
court (id_court, court_name, club_id_club (FK), court_type)
court_has_sport (court_id_court, sport_id_sport)
sport (id_sport, sport_name)

So as you can imagine, a court may allow more than one sport to be played in it (for instance, soccer and basketball).

The thing is, I'm setting up a search feature through which the user has to be able to search for a court both in their city and allowing a sport of their choice.

This can be done one at a time, or both at a time: you can first browse for all courts in your city, or all courts in all cities involving a certain sport. Now, I have both search queries independently, but my problem is when I have to perform a search using both criteria.

I've tried several methonds, such as JOINs (I don't know how to use them very well tho). The queries I use are:

Selecting a city:

Expand|Select|Wrap|Line Numbers
  1. SELECT c.id_court, c.club_id_club, c.court_name, c.court_type FROM court c,club WHERE club.id_club = c.club_id_club AND club.city='Somecity' GROUP BY id_court
Selecting a sport:

Expand|Select|Wrap|Line Numbers
  1. SELECT c.id_court, c.club_id_club, c.court_name, c.court_type FROM court_has_sport cd, sport d, cancha c WHERE cd.sport_id_sport = (SELECT id_sport FROM sport WHERE sport_name= 'Tennis' GROUP BY id_sport) AND cd.court_id_court= c.id_court GROUP BY c.id_court
Here's the important part: now I need to have a MySQL query which returns tennis courts located in Somecity (according the example).

I think (I'm pretty sure actually) both queries can be rewritten in a better way, but SQL is not my speciality.

I hope you guys can help me.

Thanks
Jan 27 '11 #1
1 1736
dgreenhouse
250 Expert 100+
I'm not sure what platform you're using for your web application is (i.e. PHP, .NET, Java, etc.), but you can dynamically create the "WHERE" clause to determine the criteria required.

i.e.
1- City
2- City and a sport
3- City and multiple sports

Obviously if there are multiple sports, you'll need to use an 'OR' or an 'IN' in the where clause.

i.e.
1- WHERE sport = 'x' OR sport = 'y'
2- WHERE sport in ('x'); WHERE sport in ('x','y')
Feb 28 '11 #2

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

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
1
by: - TW | last post by:
I need some help with a query. I have a table with inventory that I need to allow customer searches on. Based on their search criteria, a preference level is calculated; the higher the preference...
4
by: sah | last post by:
I need some help with the following query: DECLARE @SRV VARCHAR(20), @date smalldatetime SET @SRV = (select @@servername) SET @date = '20040901' select Srv_Name = @SRV, DB_Name = 'DB_NAME',...
3
by: jw56578 | last post by:
Where is the information coming from that is displayed in query profiler. Is it the command that is retrieved before it is processed, or is it what is actually processed.
0
by: kernal | last post by:
Hi, I am building a project in ASP I need a SQL query for my project. I am making "Student Fee System" for a school all is fine, but i need a search statement in which it showes only those...
10
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only...
4
by: Hemant Shah | last post by:
Folks, I am having problem with an application that uses static SQL, the application basically browses through the table given start and end key most of the time it is processed from begining to...
7
by: Techhead | last post by:
I have a date/time field with a sql format of "datetime" The actual date/time data format is MM/DD/YYYY^hh:mm:ss:pm or "1/25/2007 12:00:16 AM" Both the date and time are combined on the same field...
2
by: Ken Jones | last post by:
Table URL_3 consist of the following 2 columns of information being Record No and URL No URL 1 http:/publishing/45/100006_f.SAL_Local.html 2 ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.