| | |
Simple query I cannot figure out
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
What you need to do is build the query for only the selected options within the code block. If you build a static select query with all the options then it will return nothing because no user will select all the options.
In the code you will have to pull all the options the user selected and build the query to include only those options. Also don't use the left join from the original query as that will give null results from the Users_AOI table which you don't want.
In the code you will have to pull all the options the user selected and build the query to include only those options. Also don't use the left join from the original query as that will give null results from the Users_AOI table which you don't want.
•
•
Join Date: Jul 2008
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
If you have questions on how to dynamically build queries is C#, make a post in the C# forum.
However, like I said in my previous example, if a user pre-selected "music" and "paiting" you cannot ask:
MS SQL Syntax (Toggle Plain Text)
... WHERE AOI = music AND AOI = Painting
Even if the user preselected these two Areas Of Interest there are in two separate records, and there is no record that is music AND painting at the same time, therefore, the query (even when dynamically created) will return nothing.
Thanks.
As you mentioned in an earlier post Intersect is the way to go if you are using SQL Server 2005 or later.
Ofcourse if you are still using SQL Server 2000 you do not have access to Intersect. I have put together a query that I beleive will do the trick for you, give it a try and let me know.
You will have to build the exists portion dynamically to include the options selected.
hope this helps
sql Syntax (Toggle Plain Text)
SELECT a.name,a.userid FROM users a, aoi b, users_aoi c WHERE a.userid = c.userid AND b.aoiid = c.aoiid AND b.aoiid = option1 Intersect SELECT a.name, a.userid FROM users a, aoi b, users_aoi c WHERE a.userid = c.userid AND b.aoiid = c.aoiid AND b.aoiid = option2
Ofcourse if you are still using SQL Server 2000 you do not have access to Intersect. I have put together a query that I beleive will do the trick for you, give it a try and let me know.
sql Syntax (Toggle Plain Text)
SELECT DISTINCT a.userid,a.name FROM Users a,efx_aoi b ,users_aoi c WHERE a.userid = c.userid AND b.aoiid = c.aoiid AND EXISTS ( SELECT 1 FROM users_aoi c WHERE a.userid = c.userid AND c.aoiid = 3 AND EXISTS(SELECT 1 FROM users_aoi c WHERE a.userid = c.userid AND c.aoiid = 4) )
You will have to build the exists portion dynamically to include the options selected.
hope this helps
Last edited by cgyrob; Jul 7th, 2009 at 2:39 pm.
![]() |
Similar Threads
- Simple Multiple Update Query from HTML textboxes (PHP)
- OMG simple update query not working. PLEASE HELP! (MySQL)
- php query headers (PHP)
- Newbie - looping and array [very simple] problem (PHP)
- showing the form after insertind data (PHP)
- Help with simple shell script (Windows NT / 2000 / XP)
- microsoft access simple date query (Computer Science)
- Please help me out with MySQL query (MySQL)
Other Threads in the MS SQL Forum
- Previous Thread: Sql Server 2008 Reporting services
- Next Thread: Error while transfering text data type data through linked server
| Thread Tools | Search this Thread |





