Connecting Tech Pros Worldwide Help | Site Map

how to get multiple specific values in one row with subqueries

Newbie
 
Join Date: Sep 2009
Posts: 10
#1: Oct 9 '09
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?
Newbie
 
Join Date: Sep 2009
Posts: 10
#2: Oct 9 '09

re: how to get multiple specific values in one row with subqueries


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...
Newbie
 
Join Date: Sep 2009
Posts: 10
#3: Oct 9 '09

re: how to get multiple specific values in one row with subqueries


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
Reply

Tags
multiple, row, subquery