473,411 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Help with Select and IN

Can someone tell me options to do this statment because this one does
not work!

SELECT Name FROM tbl_J
WHERE J_ID IN
(SELECT J1, J2, J3, J4, J5, J6
FROM tbl_CJ
WHERE CJ_ID =23515) ORDER BY Name

Mar 19 '07 #1
3 1903
On Mar 19, 8:39 am, "Giorgio" <FJMarti...@googlemail.comwrote:
Can someone tell me options to do this statment because this one does
not work!

SELECT Name FROM tbl_J
WHERE J_ID IN
(SELECT J1, J2, J3, J4, J5, J6
FROM tbl_CJ
WHERE CJ_ID =23515) ORDER BY Name
I won't go into why this suggests a problem with your model, but I
think you want something like this:

SELECT Name FROM tbl_J
WHERE J_ID IN
(SELECT J1
FROM tbl_CJ
WHERE CJ_ID =23515
UNION
SELECT J2
FROM tbl_CJ
WHERE CJ_ID =23515
....
UNION
SELECT J6
FROM tbl_CJ
WHERE CJ_ID =23515) ORDER BY Name

Alternatively you could OR them all together:

SELECT Name FROM tbl_J
WHERE J_ID IN
(SELECT J1
FROM tbl_CJ
WHERE CJ_ID =23515)
OR J_ID IN
(SELECT J1
FROM tbl_CJ
WHERE CJ_ID =23515)
....
OR J_ID IN
(SELECT J6
FROM tbl_CJ
WHERE CJ_ID =23515)

ORDER BY Name

Mar 19 '07 #2
Giorgio wrote:
Can someone tell me options to do this statment because this one does
not work!

SELECT Name FROM tbl_J
WHERE J_ID IN
(SELECT J1, J2, J3, J4, J5, J6
FROM tbl_CJ
WHERE CJ_ID =23515) ORDER BY Name
You probably want to re-design tbl_CJ from this:

CJ_ID | J1 | J2 | J3 | J4 | J5 | J6
------+----+----+----+----+----+----
23515 | 1 | 2 | 3 | 4 |null|null
23516 | 5 | 6 | 7 |null|null|null

to this:

CJ_ID | J_ID
------+-----
23515 | 1
23515 | 2
23515 | 3
23515 | 4
23516 | 5
23516 | 6
23516 | 7

in which case the query becomes simple:

select j.Name
from tbl_J j
join tbl_CJ cj on cj.J_ID = j.J_ID
where cj.CJ_ID = 23515
order by j.Name

and, as an extra added bonus, you are no longer limited to a maximum of
six tbl_J records per tbl_CJ record.

Failing that, here is one of several ways to do it:

select Name
from tbl_J
where J_ID in (select J1 from tbl_CJ where CJ_ID = 23515)
or J_ID in (select J2 from tbl_CJ where CJ_ID = 23515)
or J_ID in (select J3 from tbl_CJ where CJ_ID = 23515)
or J_ID in (select J4 from tbl_CJ where CJ_ID = 23515)
or J_ID in (select J5 from tbl_CJ where CJ_ID = 23515)
or J_ID in (select J6 from tbl_CJ where CJ_ID = 23515)
order by Name
Mar 19 '07 #3
I posted this in microsoft.public.sqlserver.programming in response to
the copy you posted there. In the future if you must post to multiple
groups, include all of them in the same copy of the message.

There are a number of ways to do this, but most become long and
complicated. This may be the simplest.

SELECT Name
FROM tbl_J as A
WHERE EXISTS
(SELECT * FROM tbl_CJ as B
WHERE B.CJ_ID = 23515
AND A.J_ID IN
(B.J1, B.J2, B.J3,
B.J4, B.J5, B.J6))
ORDER BY Name

Roy Harvey
Beacon Falls, CT

On 19 Mar 2007 05:39:37 -0700, "Giorgio" <FJ********@googlemail.com>
wrote:
>Can someone tell me options to do this statment because this one does
not work!

SELECT Name FROM tbl_J
WHERE J_ID IN
(SELECT J1, J2, J3, J4, J5, J6
FROM tbl_CJ
WHERE CJ_ID =23515) ORDER BY Name
Mar 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
0
by: Pat Patterson | last post by:
I'm having serious issues with a page I'm developing. I just need some simple help, and was hoping someone might be able to help me out in here. I have a form, that consists of 3 pages of...
0
by: JonathanParker | last post by:
Hello, Wondered if you could help me with a little issue I'm having. I'm exporting some data from Access to Excel and converting into some fancy graphs. The number of series' ranges from 2 to 5....
23
by: casper christensen | last post by:
Hi I run a directory, where programs are listed based on the number of clicks they have recieved. The program with most clicks are placed on top and so on. Now I would like people to be apple to...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
1
by: upstart | last post by:
Hi everyone…this is a tough one. You guys have been such a help before, hopefully you can point me in the right direction now. I have a Report I am working on that uses a stored procedure to...
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.