473,804 Members | 2,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INNER JOINS - More than one?

113 New Member
Hi there,

I have three tables:

-properties
-propLocations
-locations

I am trying to allow each property to have more than one location. Locations holds the locations (id,name) and propLocations holds the references for each property so each row in this table has the ID of the property (propID) and the ID of the location propLocID.

I've been trying to get a results query back which has all of the properties columns and a column on the end containing a comma delimited list of the locations for that property.

This is as far as I have got:

SELECT
properties.id,
properties.name ,
properties.tenu re,
properties.size ,
properties.size Acres,
properties.pric e,
properties.rent al,
properties.onAp plication,
properties.team Member,
properties.pdf,
properties.imag e,
properties.date ,
properties.arch ive,
locations.locat ion

FROM properties
LEFT OUTER JOIN locations ON locations.id = propLocations.p ropLocID INNER JOIN propLocations ON propLocations.p ropID = properties.id;

So that returns the property columns and the locations.locat ion (name) for the property, this is ok for one location but what if there are more? How do I get a comma delimited list of the locations?

Hope you can help,

Chromis
Jan 10 '08 #1
7 1883
mwasif
802 Recognized Expert Contributor
Hi chromis,

Welcome to TSDN!!!

This is a better approach to write the JOINS.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM properties 
  2. INNER JOIN propLocations ON properties.id= propLocations.propID
  3. INNER JOIN locations ON propLocations.propLocID = locations.id;
I made this query without knowing your column names.

You can change the above query to get locations as well with the help of GROUP_CONCAT()
Expand|Select|Wrap|Line Numbers
  1. SELECT *, GROUP_CONCAT(locations.location) FROM properties 
  2. INNER JOIN propLocations ON properties.id= propLocations.propID
  3. INNER JOIN locations ON propLocations.propLocID = locations.id
  4. GROUP BY properties.id;
Post back if you face problems. But please post your table structure too.
Jan 10 '08 #2
chromis
113 New Member
Hi mwasif,

Thanks very much for your speedy reply and the warm welcome. I have attempted to use the query you gave me but ran into problems with the GROUP_CONCAT() function.

I ran the following statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT *, GROUP_CONCAT(locations.location) FROM properties
  2. INNER JOIN propLocations ON properties.id= propLocations.propID
  3. INNER JOIN locations ON propLocations.propLocID = locations.id
  4. GROUP BY properties.id;
  5.  
and it returned this error:
Expand|Select|Wrap|Line Numbers
  1. #1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near '( locations.location )
  2. FROM properties
  3. LEFT  OUTER  JOIN locati
  4.  
I've checked the syntax and it all seems ok. Would GROUP_CONCAT() throw an error if there was only one location returned for each property?

Table Structures
----------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #
  2. # Table structure for table `properties`
  3. #
  4.  
  5. CREATE TABLE properties (
  6.   id int(11) NOT NULL auto_increment,
  7.   name text NOT NULL,
  8.   number text NOT NULL,
  9.   street text NOT NULL,
  10.   town text NOT NULL,
  11.   postcode text NOT NULL,
  12.   description longtext NOT NULL,
  13.   type text NOT NULL,
  14.   tenure text NOT NULL,
  15.   size int(11) NOT NULL default '0',
  16.   sizeAcres int(11) NOT NULL default '0',
  17.   location varchar(255) NOT NULL default '',
  18.   price int(11) NOT NULL default '0',
  19.   rental int(11) NOT NULL default '0',
  20.   onApplication char(3) NOT NULL default 'no',
  21.   teamMember varchar(255) NOT NULL default '',
  22.   pdf text NOT NULL,
  23.   image text NOT NULL,
  24.   date date NOT NULL default '0000-00-00',
  25.   archive int(11) NOT NULL default '0',
  26.   PRIMARY KEY  (id)
  27. ) TYPE=MyISAM COMMENT='Properties';
  28.  
  29. #
  30. # Table structure for table `locations`
  31. #
  32.  
  33. CREATE TABLE locations (
  34.   id int(11) NOT NULL auto_increment,
  35.   pseudoname varchar(255) NOT NULL default '',
  36.   location varchar(255) NOT NULL default '',
  37.   PRIMARY KEY  (id)
  38. ) TYPE=MyISAM COMMENT='City locations for property - used for search by city';
  39.  
  40. CREATE TABLE proplocations (
  41.   propID int(11) default NULL,
  42.   propLocID int(11) default NULL
  43. ) TYPE=MyISAM COMMENT='Stores locations for properties';
  44.  
Jan 10 '08 #3
mwasif
802 Recognized Expert Contributor
GROUP_CONCAT() was added in MySQL 4.1. Which MySQL version you are using? There is no problem with the syntax (I have executed the query with your DB structure).
Jan 10 '08 #4
chromis
113 New Member
Ah here is the problem it's: 4.0.12-nt

Is there another way of achieving my goal?

Many thanks,

Chromis
Jan 10 '08 #5
mwasif
802 Recognized Expert Contributor
I don't know anyother way using only MySQL. But you can do this with the combination of MySQL and any programming language (PHP, Perl etc).

Is there any problem upgrading MySQL?
Jan 10 '08 #6
chromis
113 New Member
I don't know anyother way using only MySQL. But you can do this with the combination of MySQL and any programming language (PHP, Perl etc).

Is there any problem upgrading MySQL?
Yeah i've had a good search and haven't found anything, so I'm getting our admin to upgrade mysql, we'll see how i get on from there!

Thanks for your help,

Chromis
Jan 10 '08 #7
mwasif
802 Recognized Expert Contributor
Before upgrading, checkout the changelog and upgrade notes at MySQL site. Your applications may face Client does not support authentication protocol error after upgrading to MySQL 4.1/5.0.

Good luck and happy upgrading!
Jan 10 '08 #8

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

Similar topics

3
6420
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
8
6325
by: kieran | last post by:
Hi, I have the following sql statement. I originally had the statement with two INNER JOINS but in some situations was getting an error so changed the last INNER JOIN to a LEFT OUTER JOIN (as is seem below). This seemed to work but i am unsure why and would like to know in case it falls over again. Why did the two INNER JOINS not work, and am I correct to use the LEFT OUTER JOIN in this context. As you can see the table 'tblStaff1_2'...
4
2048
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it ? thanks
2
1628
by: gkellymail | last post by:
the following query works fine: select link.idx, link.x_table, link.x_id_a, link.x_id_z, a.strandid, b.strandid from link_detail, link, strand A, strand B where link_detail.x_table = 'enclosure' and link_detail.x_id = 3 and link.idx = link_detail.linkidx and A.strandid = link.x_id_a and B.strandid = link.x_id_z would someone please convert this to a more efficient query using inner
6
9320
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: tblEmployees empId -- EmpName -- EmpRole -- EmpManager -------....------------.... ---------....--------------- 1........ dan yella..........1..........2
52
6363
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
3
16513
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so all data from the separate tables is shown in a view (instead of the reference id's pointing to the separate tables...) I have some troubles formulating the SQL statement: I tried:
2
1545
by: Rick | last post by:
VS 2005 & Firebird DB 1.5 I have a query for Customer orders that has several inner joins to show things like Terms name (from termsnum) and Sales Rep name (from repnum) etc. I have written the custom SQL insert statement and this works with no problem, but I want to get the updated values back from the DB for the inner joins. When I add a new row, it appears in a datagridview, but all the inner join fields are missing. I know I could...
12
13192
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
9
11923
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example: http://www.codinghorror.com/blog/archives/000976.html
0
9715
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
9595
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
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
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...
0
6869
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();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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.