473,396 Members | 2,154 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,396 software developers and data experts.

how to get multiple specific values in one row with subqueries

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT servicesid,short_name, long_name 
  2. FROM tg_services 
  3. WHERE servicesid IN 
  4. (SELECT DISTINCT service_id FROM tg_srv_links 
  5. WHERE service_id IN 
  6. (SELECT DISTINCT service_id 
  7. FROM tg_srv_links 
  8. WHERE option_id = 9) AND option_id = 40 AND option_id = 27 AND option_id = 3) 
  9. AND s_approved=1 
  10. AND long_name <> '' 
  11. ORDER BY `long_name` 
  12. LIMIT 0, 5
The part I am having problems with is

Expand|Select|Wrap|Line Numbers
  1. option_id = 40 AND option_id = 27 AND option_id = 3
The tg_srv_links table looks like this
Expand|Select|Wrap|Line Numbers
  1.    row_id     |   service_id   |   option_id
  2. -----------------------------------------------
  3.       3       |      9         |    40
  4. -----------------------------------------------
  5.       4       |      9         |    27
  6. -----------------------------------------------
  7.       5       |      9         |    3
  8. -----------------------------------------------
  9.       6       |      6         |    40
  10. -----------------------------------------------
  11.       7       |      6         |    27
  12. -----------------------------------------------
  13.       8       |      6         |    22
  14. -----------------------------------------------
  15.       9       |      24        |    10
  16. -----------------------------------------------
  17.       10      |      24        |    27
  18. -----------------------------------------------
  19.       11      |      24        |    40
  20. -----------------------------------------------
  21.       12      |      24        |    3
  22.  
What I need to do is find the specified service_id that has the exact 3 option_id's

In the table example above, the query should return 9 and 24 for the service_id's, since 9 and 24 both contain option_id's with 40, 27 and 3


anyone have any suggestions how to proceed?
Oct 9 '09 #1
2 2415
I tried the following but it doesn't work either, it return #1052 - Column 'option_id' in field list is ambiguous

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT servicesid, short_fr, name_fr
  2. FROM tg_services
  3. WHERE servicesid
  4. IN (
  5.  
  6. SELECT DISTINCT service_id
  7. FROM tg_srv_links
  8. WHERE service_id
  9. IN (
  10.  
  11. SELECT DISTINCT service_id
  12. FROM tg_srv_links
  13. WHERE option_id =9
  14. )
  15. AND option_id
  16. IN (
  17.  
  18. SELECT DISTINCT option_id
  19. FROM tg_srv_links AS L1
  20. JOIN tg_srv_links AS L2 ON L1.service_id = L2.service_id
  21. JOIN tg_srv_links AS L3 ON L1.service_id = L3.service_id
  22. WHERE L1.option_id =3
  23. AND L2.option_id =40
  24. AND L3.option_id =27
  25. )
  26. )
  27. LIMIT 0 , 30
Not sure if I am on the right path to a solution...
Oct 9 '09 #2
I think I got it working properly as it returns results but I'll have to investigate each individual result and see if it's supposed to be there

here's what I ended up with
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT servicesid, short_en, name_en
  2. FROM tg_services
  3. WHERE servicesid
  4. IN (
  5.  
  6. SELECT DISTINCT service_id
  7. FROM tg_srv_links
  8. WHERE service_id
  9. IN (
  10.  
  11. SELECT DISTINCT service_id
  12. FROM tg_srv_links
  13. WHERE option_id =40
  14. )
  15. AND service_id
  16. IN (
  17.  
  18. SELECT DISTINCT LA.service_id
  19. FROM tg_srv_links AS LA
  20. JOIN tg_srv_links AS L1 ON LA.service_id = L1.service_id
  21. JOIN tg_srv_links AS L2 ON LA.service_id = L2.service_id
  22. WHERE LA.option_id =10
  23. AND L1.option_id =28
  24. AND L2.option_id =33
  25. )
  26. )
  27. LIMIT 0 , 30
Oct 9 '09 #3

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

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
2
by: KathyB | last post by:
Hi, figured out where I was going wrong in my post just prior, but is there ANY way I can assign several variables to then use them in an Update statement, for example (this does not work): ...
8
by: charles.amith | last post by:
I have 2 tables: LOCATION and ELEVATION In location, I would like to find the record with the max value for field: DATE1 In elevation, I would like to find the record with the max value for...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
by: jimfortune | last post by:
In: http://groups.google.com/group/comp.databases.ms-access/msg/60d7faa790c65db1 james.ea...@gmail.com said: :Ok, I've answered the first half of my own question. From a MSDN :article, I...
4
by: Papachumba | last post by:
Hi guys, i have a little problem and was wandering if anyone can help. I just need my query modified a little bit to allow some extra values in there... Tables: offers Table OfferID...
9
by: Mohd Al Junaibi | last post by:
Hello all, my first post here...hope it goes well. I'm currently working on stored procedure where I translated some reporting language into T-SQL The logic: I have a group of tables...
2
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field...
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
1
by: jghouse | last post by:
Everyone, I am just starting my work with subqueries and I am a little lost on how to handle the syntax on one part. I have a query that will contain some subqueries for the purpose of counting...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.