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
7 1883 mwasif 802
Recognized Expert Contributor
Hi chromis,
Welcome to TSDN!!!
This is a better approach to write the JOINS. - SELECT * FROM properties
-
INNER JOIN propLocations ON properties.id= propLocations.propID
-
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() - SELECT *, GROUP_CONCAT(locations.location) FROM properties
-
INNER JOIN propLocations ON properties.id= propLocations.propID
-
INNER JOIN locations ON propLocations.propLocID = locations.id
-
GROUP BY properties.id;
Post back if you face problems. But please post your table structure too.
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: -
SELECT *, GROUP_CONCAT(locations.location) FROM properties
-
INNER JOIN propLocations ON properties.id= propLocations.propID
-
INNER JOIN locations ON propLocations.propLocID = locations.id
-
GROUP BY properties.id;
-
and it returned this error: -
#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 )
-
FROM properties
-
LEFT OUTER JOIN locati
-
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
---------------------------------------------------------- -
#
-
# Table structure for table `properties`
-
#
-
-
CREATE TABLE properties (
-
id int(11) NOT NULL auto_increment,
-
name text NOT NULL,
-
number text NOT NULL,
-
street text NOT NULL,
-
town text NOT NULL,
-
postcode text NOT NULL,
-
description longtext NOT NULL,
-
type text NOT NULL,
-
tenure text NOT NULL,
-
size int(11) NOT NULL default '0',
-
sizeAcres int(11) NOT NULL default '0',
-
location varchar(255) NOT NULL default '',
-
price int(11) NOT NULL default '0',
-
rental int(11) NOT NULL default '0',
-
onApplication char(3) NOT NULL default 'no',
-
teamMember varchar(255) NOT NULL default '',
-
pdf text NOT NULL,
-
image text NOT NULL,
-
date date NOT NULL default '0000-00-00',
-
archive int(11) NOT NULL default '0',
-
PRIMARY KEY (id)
-
) TYPE=MyISAM COMMENT='Properties';
-
-
#
-
# Table structure for table `locations`
-
#
-
-
CREATE TABLE locations (
-
id int(11) NOT NULL auto_increment,
-
pseudoname varchar(255) NOT NULL default '',
-
location varchar(255) NOT NULL default '',
-
PRIMARY KEY (id)
-
) TYPE=MyISAM COMMENT='City locations for property - used for search by city';
-
-
CREATE TABLE proplocations (
-
propID int(11) default NULL,
-
propLocID int(11) default NULL
-
) TYPE=MyISAM COMMENT='Stores locations for properties';
-
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).
Ah here is the problem it's: 4.0.12-nt
Is there another way of achieving my goal?
Many thanks,
Chromis
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?
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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'...
|
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
|
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
|
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
| |
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
|
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:
|
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...
|
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
|
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
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |