473,394 Members | 1,746 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.

Multiple JOINS and controlling duplicates

I'm kind of comfortable with basic joins, but a current project requires a complex query of many tables. The GROUP_CONCAT(DISTINCT ...) function has been very useful as returning my values as comma delimited values when joining multiple tables. I have one table called 'floorplans' which has two fields (floorplan_jpg & floorplan_pdf), I'd like each of these fields to return arrays of the same length (they have the same # of values in the data model). When i use GROUP_CONCAT(DISTINCT...) on these fields it returns the arrays as I'd like except for that it eliminates 'duplicate' empty fields from the result. How do I collect only the unique values from a field, while also collecting multiple empty values?

Database:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE properties (
  2. property_id TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT, 
  3. property_name VARCHAR(36),
  4. PRIMARY KEY (property_id)
  5. );
  6.  
  7. CREATE TABLE residences (
  8. residence_id SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  9. property_id TINYINT(3) UNSIGNED NOT NULL,
  10. residence_floor TINYINT(3) UNSIGNED NOT NULL,
  11. residence_line VARCHAR(2) NOT NULL,
  12. residence_type ENUM('studio','alcove','1 bedroom','2 bedroom','3 bedroom'),
  13. residence_bathrooms VARCHAR(3) NOT NULL,
  14. residence_rent DECIMAL(6,2),
  15. residence_availabilitydate DATE,
  16. residence_status ENUM('off','on'),
  17. PRIMARY KEY (residence_id)
  18. );
  19.  
  20. CREATE TABLE features (
  21. feature_id SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  22. property_id TINYINT(3) UNSIGNED NOT NULL,
  23. feature_name VARCHAR(36),
  24. PRIMARY KEY (feature_id)
  25. );
  26.  
  27. CREATE TABLE residences_x_features (
  28. residence_id SMALLINT(5) UNSIGNED NOT NULL,
  29. feature_id SMALLINT(5) UNSIGNED NOT NULL,
  30. PRIMARY KEY (residence_id,feature_id)
  31. );
  32.  
  33. CREATE TABLE incentives (
  34. incentive_id SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  35. property_id TINYINT(3) UNSIGNED NOT NULL,
  36. incentive_name VARCHAR(36),
  37. PRIMARY KEY (incentive_id)
  38. );
  39.  
  40. CREATE TABLE residences_x_incentives (
  41. residence_id SMALLINT(5) UNSIGNED NOT NULL,
  42. incentive_id SMALLINT(5) UNSIGNED NOT NULL,
  43. PRIMARY KEY (residence_id,incentive_id)
  44. );
  45.  
  46. CREATE TABLE floorplans (
  47. floorplan_id MEDIUMINT(7) UNSIGNED NOT NULL AUTO_INCREMENT,
  48. residence_id SMALLINT(5) UNSIGNED NOT NULL,
  49. floorplan_jpg VARCHAR(36),
  50. floorplan_pdf VARCHAR(36),
  51. floorplan_label VARCHAR(48),
  52. PRIMARY KEY (floorplan_id)
  53. );
  54.  
  55. CREATE TABLE views (
  56. view_id MEDIUMINT(7) UNSIGNED NOT NULL AUTO_INCREMENT,
  57. residence_id SMALLINT(5) UNSIGNED NOT NULL,
  58. view_jpg VARCHAR(36),
  59. view_label VARCHAR(48),
  60. PRIMARY KEY (view_id)
  61. );
Query:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. residences.residence_id,residences.residence_floor,residences.residence_line,residences.residence_type,residences.residence_bathrooms,residences.residence_availabilitydate,residences.residence_rent,residences.residence_status, 
  3. GROUP_CONCAT(DISTINCT floorplans.floorplan_label ORDER BY floorplans.floorplan_id) AS floorplan_labels, 
  4. GROUP_CONCAT(floorplans.floorplan_pdf ORDER BY floorplans.floorplan_id) AS floorplan_pdfs, 
  5. GROUP_CONCAT(floorplans.floorplan_jpg ORDER BY floorplans.floorplan_id) AS floorplan_jpgs, 
  6. GROUP_CONCAT(DISTINCT views.view_jpg) AS view_jpgs, 
  7. GROUP_CONCAT(DISTINCT views.view_label) AS view_labels, 
  8. GROUP_CONCAT(DISTINCT features.feature_name) AS featurelist,
  9. GROUP_CONCAT(DISTINCT incentives.incentive_name) AS incentivelist 
  10. FROM residences 
  11. LEFT JOIN floorplans ON residences.residence_id = floorplans.residence_id 
  12. LEFT JOIN views ON views.residence_id = residences.residence_id 
  13. LEFT JOIN residences_x_features ON residences_x_features.residence_id = residences.residence_id 
  14. LEFT JOIN features ON features.feature_id = residences_x_features.feature_id 
  15. LEFT JOIN residences_x_incentives ON residences_x_incentives.residence_id = residences.residence_id 
  16. LEFT JOIN incentives ON incentives.incentive_id = residences_x_incentives.incentive_id 
  17. WHERE residences.property_id = 1 
  18. GROUP BY residences.residence_id 
  19. ORDER BY residences.residence_floor DESC,residences.residence_line ASC
Results:
Expand|Select|Wrap|Line Numbers
  1. +--------------+------------------------------------------------+-------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------+--------------------+-------------------+---------------+
  2. | residence_id | floorplan_labels                               | floorplan_pdfs                                                                            | floorplan_jpgs                                                                                                                                                                                                                            | view_jpgs                              | view_labels        | featurelist       | incentivelist |
  3. +--------------+------------------------------------------------+-------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------+--------------------+-------------------+---------------+
  4. |          301 | a,b,c                                          | ,,,,,,6_301_59.pdf,6_301_59.pdf,6_301_59.pdf,6_301_59.pdf,6_301_59.pdf,6_301_59.pdf,,,,,, | 6_301_58.jpg,6_301_58.jpg,6_301_58.jpg,6_301_58.jpg,6_301_58.jpg,6_301_58.jpg,6_301_59.jpg,6_301_59.jpg,6_301_59.jpg,6_301_59.jpg,6_301_59.jpg,6_301_59.jpg,6_301_60.jpg,6_301_60.jpg,6_301_60.jpg,6_301_60.jpg,6_301_60.jpg,6_301_60.jpg | 6_301_47.jpg,6_301_46.jpg,6_301_51.jpg | TEST 2,TEST,TEST 3 | Marble Lobby,test | NULL          |
  5. |          302 | test,Test Floorplan,PDF test,Double Test,x,y,z | ,,,,6_302_41.pdf,6_302_41.pdf,6_302_42.pdf,6_302_42.pdf,,,,,,                             | 6_302_39.jpg,6_302_39.jpg,6_302_40.jpg,6_302_40.jpg,,,6_302_42.jpg,6_302_42.jpg,6_302_52.jpg,6_302_52.jpg,6_302_53.jpg,6_302_53.jpg,6_302_54.jpg,6_302_54.jpg                                                                             | 6_302_53.jpg,6_302_52.jpg              | y,x                | NULL              | NULL          |
  6.  
Basically, if I add DISTINCT to the floorplan_jpg and floorplan_pdf SELECT, I get close to what I need, except it eliminates duplicate empty values, which i want to maintain. If I remove DISTINCT, I get exponential filename duplicates due to the multiple joins. I've been strugging with this for days, any input would be greatly appreciated. Thanks!
Feb 27 '08 #1
2 3138
chaarmann
785 Expert 512MB
What you wrote is very hard to understand.
Can you please:
1.) simplify the problem by leaving out all tables and parts of the SQL that have nothing to do with the problem itself. It's a help for you to focus more on the real problem, and of couse for us so we can understand easily and not waste a long time in doing so.
2.) list some test data that you have in your tables. Without data, nobody can try your problem on his own machine and verify his developed solution.

A general tip:
DISTINCT is not the only way to remove duplicate records from the result. Especially if you want to leave out some columns in your decision about: if two rows are equal or not. This sound like your case.
Usually you use for that purpose:
select min(id) from... group by ...HAVING count(*) > 1
to pick the first one of all duplicate rows that you want to keep, and filter out all the others with ... where exists () ...
Feb 28 '08 #2
ronverdonk
4,258 Expert 4TB
This thread was almost hijacked by eviephillips. That problem has been split off and continues as a new thread http://www.thescripts.com/forum/thread778362.html

moderator
Mar 4 '08 #3

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

Similar topics

1
by: Prem | last post by:
Hi All Database Gurus, I am trying to write code which will produce all the possible valid queries, given tables and join information for tables. Right now i am just trying to construct all the...
1
by: ankush | last post by:
I have a table heirarchy of around 5 tables. At places where i have to do a search with criteria ranging on all these tables, will it be advisable to have a join on all the tables. A couple of tables...
7
by: SeaviewBlue | last post by:
Howdy folks, I am new to the site and new to joins, so I'm hoping I can get some much needed assistance. Using ASP, VBscript & an Access 2003 db, I am building a page to display results from...
2
by: narendra vuradi | last post by:
Hi I have a requirement where in i haev to convert the SQL from Oracle to the one which will run on the SQL server. in the Oracle Query i am doing multiple joins, between some 13 tables. and...
2
by: eviephillips | last post by:
I am having a similar problem. I am using Dreamweaver (MySQL, PHP) and their advanced recordset to select the tables and fields. The recordsets are pulling all the pertinent records. However, when...
3
by: modernshoggoth | last post by:
G'day all, I'm trying to update a single table (containing plans for flights) with information from the same and other tables (containing info for organisations and locations). tblFlightPlans...
13
beacon
by: beacon | last post by:
I'm working on creating a dynamic report that is based on a crosstab query. It's similar to the method described at the following link: http://support.microsoft.com/kb/328320 I've tweaked the...
0
by: kalinda | last post by:
I am trying to make multiple joins between two tables and am at a loss. I've tried a variety of things with this being the latest, but it pulls all records multiple times. Basically I have a main...
3
by: John Roberts | last post by:
Hi, Is it possible to access a tables data over multiple joins? For instance, for each row of Table A, I want the associated data from Table D. Is this possible in access? If it's not, could you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.