473,471 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Save results from query in field for WHERE IN statement

9 New Member
I would like to query a table, and save the results into 1 field of a results table as below:

Table1:
StudentID FirstName LastName
JOHND John Doe
JANED Jane Doe
ROJERD Rojer Doe
JOHNNYJ Johnny Johnson

Table2:
SearchId Results
1 JOHND, JANED, ROJERD

Table 2 populated by 2 queries
SELECT StudentID FROM Table1 WHERE LastName = 'Doe'
i build the comma separated list from that query and then insert into table2
insert into Table2 (Results) VALUES (@StudentList)


Then I would like to select from the student table where student in results field like

SELECT * FROM Table1 WHERE StudentID IN (SELECT Results FROM Table2 WHERE SearchID = @SearchID)

I've tried adding single quotes to each side of each result, but that does not work either...
Oct 5 '10 #1

✓ answered by ck9663

Well you have 2 options...

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM Table1 t1
  3. where exists (select 1 from FROM Table2 t2 WHERE t2.SearchID = @SearchID and charindex(t1.studentid, t2.results) > 1
  4.  
  5.  
OR..
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT t1.* 
  3. FROM Table1 t1
  4. inner join t2.SearchID = @SearchID and charindex(t1.studentid, t2.results) > 1
  5.  
  6.  
I just use whatever table you have already. So I assume you have the result as delimited string. As I said, INNER JOIN will do.

Happy Coding!!!

~~ CK

6 1795
ck9663
2,878 Recognized Expert Specialist
Your entire requirement can be sum up into a single query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM Table1 WHERE LastName = 'Doe'
  3.  
  4.  
If you could give us more detail, like why do you need to put it into a separate table only to be used as searched argument when in fact it'll give the same result as the above query?

We might be able to suggest more efficient solution.

Happy Coding!!!


~~ CK
Oct 5 '10 #2
C Sharper
9 New Member
I need it for saved search results. There are many more fields a person will be able to search by as well... I just did this as a simple example of what I am looking for. I am saving the search results because the items in the tables change so frequently.

Basically I just want a person to be able to fill out their search fields, perform the search, and save the results in a table -- which I can use later to display the results...
Oct 5 '10 #3
ck9663
2,878 Recognized Expert Specialist
If that's the case, it might be better to do an inner join.

Good Luck!!!

~~ CK
Oct 5 '10 #4
C Sharper
9 New Member
I don't see how an inner join will help with my situation. I want to save a string (like an array of results) to one field in the database. Then I want to be able to use that string for my WHERE IN statement. I need to know how to format that string when I save it so I can use it in my SELECT WHERE IN statement... I have to save the results in the table so users can see their search results 6 months from now
Oct 7 '10 #5
ck9663
2,878 Recognized Expert Specialist
Well you have 2 options...

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM Table1 t1
  3. where exists (select 1 from FROM Table2 t2 WHERE t2.SearchID = @SearchID and charindex(t1.studentid, t2.results) > 1
  4.  
  5.  
OR..
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT t1.* 
  3. FROM Table1 t1
  4. inner join t2.SearchID = @SearchID and charindex(t1.studentid, t2.results) > 1
  5.  
  6.  
I just use whatever table you have already. So I assume you have the result as delimited string. As I said, INNER JOIN will do.

Happy Coding!!!

~~ CK
Oct 7 '10 #6
C Sharper
9 New Member
@ ck9663 - I used your first option, and it looks to be working as I needed... Thanks!
Oct 7 '10 #7

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

Similar topics

4
by: Tom Walker | last post by:
I cannot get the WHERE statement to work correctly unless I use a literal with the LIKE. I want to use a working storage data name so that I can vary the WHERE statement. Example that works: ...
2
by: Ellen Manning | last post by:
Using A2K. In my table I have the field "Grant" which can have a value or be null. I have a query that counts the number of records and has a Where clause on the Grant field. The query won't...
2
by: visionstate | last post by:
Hi there, Is there a way of searching a sub-form (reading from a query) without writing what is exactly contained in the query field. For example if I had a 'Finance Manager' in the query but just...
1
by: blue875 | last post by:
SELECT Service.Name As AtlasProductName, IIf(Service.Name Like "1004*", "1004/URAR", IIf(Service.Name Like "2055E CompPhots&Land", "2055 Ext w/Comp Pics", IIf(Service.Name Like "2055I...
2
by: awu | last post by:
All: I have a query that has 5 fields and around 15500 records. I need to save it to a excel file that has two sheets. First sheet saves all the 15500 records. Second sheet saves around 7500...
9
imrosie
by: imrosie | last post by:
Hello Experts, It's going to take one to figure this out. My Order form is supported by a query of two tables (Customers and Orders). There is a subform within the Form for entering in the new...
2
ntxsoft
by: ntxsoft | last post by:
Hello everybody, I have a problem while I am trying to execute multiple query in a statement. My query like that DROP TABLE IF EXISTS query; CREATE TEMPORARY TABLE query SELECT...
2
by: progvar | last post by:
Hi! i am running the query but it is showing the error of syntax, i m accessing this table from sql server & my query is squery = "SELECT * FROM New_Rec where From_Date > = " & Format(DTPFrom &...
8
by: MLH | last post by:
Sometimes it works and sometimes it crashes. If I want "Today is " & Date$ & "." to appear in a query field, why might it work sometimes and not others? Would I be better to call a FN? Say,...
5
by: MARIEDB2 | last post by:
Hello, I am struggling to build a query on a DB2 db wich does an update for multiple fields which are the result of a select query with where clause (using multiple tables in the condition). ...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.