Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Trouble with MySQL query

Posted on 2009-04-29
8
Medium Priority
?
162 Views
Last Modified: 2012-05-06
The attached code basically builds a list of...

Main Categories
- Sub Categories
--- Company Name

I'm needing to extend the query to not only pull the above data but to pull Main Categories with a '1' value under the reference_cat column. (AND t1.reference_cat = '1') in my code.

As of now the call will only pull main categories with sub categories and company names associated with it.

<cfquery name="get_categories" datasource="#application.dsn#" username="#application.dsnUsername#" password="#application.dsnPassword#">
	SELECT 
    	*
	FROM
    	bg_categories_2010 t1, bg_sub_categories_2010 t2, bg_advertiser_link_2010 t3, bg_advertiser_profile_2010 t4
    WHERE (t1.id_category = t2.category_id AND 
    t3.sub_cat_id = t2.id_sub_category AND 
    t4.advertiser_id = t3.advertiser_id AND 
    t4.active = '1') AND t1.reference_cat = '1'
    ORDER BY t1.category, t2.sub_category, t4.company_name
</cfquery>

Open in new window

0
Comment
Question by:brihol44
  • 5
  • 3
8 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 24265506
Hi brihol44,

You will need to change it use an OUTER/LEFT join so that main categories without sub categories etc are extracted.  e.g below

lwadwell
SELECT *
  FROM bg_categories_2010 t1
  LEFT JOIN bg_sub_categories_2010 t2
         ON t1.id_category = t2.category_id
  LEFT JOIN bg_advertiser_link_2010 t3
         ON t2.id_sub_category = t3.sub_cat_id
  LEFT JOIN bg_advertiser_profile_2010 t4
         ON t3.advertiser_id = t4.advertiser_id
            AND t4.active = '1'
 WHERE t1.reference_cat = '1'
 ORDER BY t1.category, t2.sub_category, t4.company_name

Open in new window

0
 

Author Comment

by:brihol44
ID: 24265696
Nice work! I'm able to get the list that I need with...

<cfquery name="get_categories" datasource="#application.dsn#" username="#application.dsnUsername#" password="#application.dsnPassword#">
      SELECT *
      FROM bg_categories_2010 t1
      LEFT JOIN bg_sub_categories_2010 t2
             ON t1.id_category = t2.category_id
      LEFT JOIN bg_advertiser_link_2010 t3
             ON t2.id_sub_category = t3.sub_cat_id
      LEFT JOIN bg_advertiser_profile_2010 t4
             ON t3.advertiser_id = t4.advertiser_id
                AND t4.active = '1'
     
     ORDER BY t1.category, t2.sub_category, t4.company_name
</cfquery>


however I only need to show categories to sub-categories if there is a company linked to that sub-category or if the main category has a '1' value for reference_cat.

so if I have the code that you put...

SELECT *
  FROM bg_categories_2010 t1
  LEFT JOIN bg_sub_categories_2010 t2
         ON t1.id_category = t2.category_id
  LEFT JOIN bg_advertiser_link_2010 t3
         ON t2.id_sub_category = t3.sub_cat_id
  LEFT JOIN bg_advertiser_profile_2010 t4
         ON t3.advertiser_id = t4.advertiser_id
            AND t4.active = '1'
 WHERE t1.reference_cat = '1'
 ORDER BY t1.category, t2.sub_category, t4.company_name

I only get the listings with reference_cat = '1' when I need to get them as well as all others.
0
 

Author Comment

by:brihol44
ID: 24265719
I only get the listings with reference_cat = '1' when I need to get them as well as all others with company listings from categories > sub-categories.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:lwadwell
ID: 24265735
brihol44,

This is a twaek to the WHERE clause.

At the momemnt it has an explict ... t1.reference_cat = '1'  you will need to extend that to include others ... maybe

t1.reference_cat = '1' OR t2.category_id IS NOT NULL

lwadwell
0
 

Author Comment

by:brihol44
ID: 24265829
Getting closer! Got the list I need... I'm trying to figure out how to eliminate the main categories 2 sub-categories that don't have any company listings under them but still pulling the main categories that have the reference_cat '1' value.

Thanks

B
0
 

Author Comment

by:brihol44
ID: 24265831
Current call...
<cfquery name="get_categories" datasource="#application.dsn#" username="#application.dsnUsername#" password="#application.dsnPassword#">
	SELECT *
      FROM bg_categories_2010 t1
      LEFT JOIN bg_sub_categories_2010 t2
             ON t1.id_category = t2.category_id
      LEFT JOIN bg_advertiser_link_2010 t3
             ON t2.id_sub_category = t3.sub_cat_id
      LEFT JOIN bg_advertiser_profile_2010 t4
             ON t3.advertiser_id = t4.advertiser_id
                AND t4.active = '1'
     WHERE t1.reference_cat = '1' OR t2.category_id IS NOT NULL
     ORDER BY t1.category, t2.sub_category, t4.company_name
</cfquery>

Open in new window

0
 
LVL 25

Accepted Solution

by:
lwadwell earned 2000 total points
ID: 24265856
lwadwell,

try
t1.reference_cat = '1' OR t4.advertiser_id IS NOT NULL

The idea behind the IS NOT NULL is to return those where there is a JOIN (i.e. rows exist) ... you need to test to the right table.  I was not sure which one you wanted.

lwadwell
0
 

Author Closing Comment

by:brihol44
ID: 31576221
You da bomb! Thanks....
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question