Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 20th, 2008, 11:54 PM
Authorized User
 
Join Date: May 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default can I filter rows based on this type of constraint

Hi,

Can I do filtering based on Table 2 where I have only a select statement on Table 1, by using the WHERE part of the statement? E.g. I have a framework which creates a view of People, but you can pass the WHERE clause. So filtering for attributes that are on the People table is OK.

   SELECT * FROM `people` WHERE (cars_id IS NOT NULL) ________A

BUT what I really want to do is get a filtered list of People for which they have a registered car. This would normally be possible by:

    SELECT * FROM `people` LEFT OUTER JOIN `cars` ON `cars`.id = `people`.cars_id WHERE (cars.registered = 1); ________B

However I only have access to the WHERE cause. So is there anyway to modify the SQL for the WHERE clause in the first SQL statement above (A) such that I can do such filtering?

Any ideas?

thanks
 
Old August 21st, 2008, 01:27 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Why do you think you need a LEFT JOIN?

The LEFT JOIN will get *all* people, no matter whether they are associated with any CAR or not.

Besides, you said that you "only have access to the WHERE cause," so how would you be able to specify which kind of JOIN is used???

Anyway, yes, I think you could do this with *ONLY* a WHERE clause.

whereClause := cars_id IN ( SELECT id FROM cars WHERE registered = 1 )

That is, your full query would become

Code:
    SELECT * FROM people WHERE (
        cars_id IN ( SELECT id FROM cars WHERE registered = 1 )
        )

But since I'm pretty sure this isn't your *real* problem, you may need to show real code instead of fake code to get a real answer.
 
Old August 21st, 2008, 04:31 PM
Authorized User
 
Join Date: May 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thats exactly what I was fishing for Old Pendant. thanks!

the background is working around the constraint of active scaffold, for Ruby on Rails. http://activescaffold.com/

thanks again





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter rows from checkbox almaaly C# 2005 0 August 3rd, 2008 10:54 AM
Subform Filter based on Multiselect Combo boxes natwong Access 2 November 16th, 2006 10:05 AM
Filter a subform based on value entered mackoy Access 1 August 25th, 2006 12:27 PM
How to filter records based on selection of combox method Access 1 June 30th, 2005 03:34 AM
Filter Based Upon Logged In User LandOfToz BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 August 31st, 2004 12:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.