[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

08/16/2009 at 07:46AM PDT, ID: 24656322
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.5

Fairly complex question about joining tables

Asked by poisa in MySQL Server, Databases Miscellaneous, SQL Query Syntax

Tags: mysql, MySQL

I'm having trouble with one query. It's not returning exactly what I want and I sort of know conceptually what's wrong but don't know enough SQL to fix it.
This query is part of a very big system of around 50 tables. I'm including the ones in question here in a watered down version to avoid clutter.
Basically this part of the system manages a directory for a web page and allows the admin to "tag" directory items. Each directory item can have more than one tag (they usuarlly have 10 or 15). Directory items are also assigned a "location" which is basically a reference to a physical place so that directory items can then be filtered by location and by tags.

Here's the table layout:

directoryitem: This is where the info I want to list and filter is
contact info: Has general contact information for an item (and many other things in the system). Also contains a location.
location: Describes the physical placement of something (country, town, village, etc)
directoryitemtags: Relation table between tags and directory item <-- This is where I think my problem is

So basically I am given a location id and a list of tag ids and I need to be able to list all the directory items that match.
Remember: each directory item only has ONE location (which resides in the contact info table) and can have MANY tags.



Any help is GREATLY appreciated!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
/* This works as expected (note that there is only one tag) */
 
SELECT DISTINCT
  (di.id)
FROM directoryitem di,
  location l,
  contactinfo ci,
  directoryitemtags dit
WHERE di.contactInfoId = ci.id
    AND l.id = ci.locationId
    AND dit.directoryItemId = di.id
    AND dit.tagId = 373
    AND l.countryId = 225
ORDER BY di.businessName ASC
 
 
 
/* This doesn't work as expected (note that there is more than one tag) */
 
SELECT DISTINCT
  (di.id)
FROM directoryitem di,
  location l,
  contactinfo ci,
  directoryitemtags dit
WHERE di.contactInfoId = ci.id
    AND l.id = ci.locationId
    AND dit.directoryItemId = di.id
    AND (dit.tagId = 342 AND dit.tagId = 373)
    AND l.countryId = 225
ORDER BY di.businessName ASC
 
 
 
 
/*
Table schemas
*/
 
CREATE TABLE `directoryitem` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `contactInfoId` int(10) unsigned NOT NULL,
 
  etc, etc...
 
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM 
 
 
 
 
CREATE TABLE `location` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `countryId` int(10) unsigned default NULL,
  `cityId` int(10) unsigned default NULL,
  `communityAreaId` int(10) unsigned default NULL,
  `townId` int(10) unsigned default NULL,
  `villageId` int(10) unsigned default NULL,
 
   etc, etc...
 
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM 
 
 
 
 
 
CREATE TABLE `contactinfo` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `email` varchar(100) NOT NULL,
  `locationId` int(10) unsigned NOT NULL,
 
   etc, etc...
  
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM 
 
 
 
 
CREATE TABLE `directoryitemtags` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `directoryItemId` int(10) unsigned NOT NULL,
  `tagId` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM
[+][-]08/16/09 08:08 AM, ID: 25109217

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 08:10 AM, ID: 25109221

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 09:38 AM, ID: 25109473

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/16/09 11:01 AM, ID: 25109738

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 11:47 AM, ID: 25109896

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 11:55 AM, ID: 25109941

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 01:00 PM, ID: 25110218

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/16/09 04:07 PM, ID: 25110826

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 04:15 PM, ID: 25110851

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 04:32 PM, ID: 25110911

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 04:45 PM, ID: 25110934

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/16/09 04:58 PM, ID: 25110963

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/17/09 07:49 AM, ID: 25114944

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/17/09 11:00 AM, ID: 25116723

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MySQL Server, Databases Miscellaneous, SQL Query Syntax
Tags: mysql, MySQL
Sign Up Now!
Solution Provided By: acperkins
Participating Experts: 3
Solution Grade: A
 
 
[+][-]08/18/09 06:15 AM, ID: 25122817

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625