2

BubQuery always return the empty result if the query is correct.

1st Query:

SELECT * FROM `user` WHERE user_age IN(1,22,34);

Result:

enter image description here

2nd Query:

SELECT GROUP_CONCAT(user_age_list) AS user_age FROM `user_detail` WHERE id='1';

Result: enter image description here

I am try:

SELECT * FROM `user` WHERE user_age IN(SELECT GROUP_CONCAT(user_age_list) AS user_age FROM `user_detail` WHERE id='1');

Sqlfiddle: http://sqlfiddle.com./#!9/d6515f/3 //This is a sample table.

Above the query is always return the empty rows. But each of query return the result if its run single. Really I don't know where is the error. Please update the answer or suggest me.

Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46

2 Answers2

1

Avoid Use of GROUP_CONCAT

SELECT * 
FROM `user` 
WHERE user_age IN(SELECT user_age_list FROM `user_detail` WHERE id='1');

UPDATED

SELECT * 
FROM `user` u 
WHERE EXISTS (SELECT 1 FROM `user_detail` ud  WHERE id='1' AND ud.user_age_list = u.user_age)
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
0
SELECT * 
FROM user
WHERE user_age IN 
(SELECT user_age_list user_age 
 FROM user_detail
 WHERE id='1');

You don't need the group concat here. The engine knows the result set is an inclusive list without the group concat.

What it is trying to do is compare the '1,22,34' to each user_age which is why you get no results.

1 <> '1,22,34'
22 <> '1,22,34'
34 <> '1,22,34' 

thus no results.

xQbert
  • 34,733
  • 2
  • 41
  • 62
  • It's return the empty rows.It consider the single value within single quotes. – Ramalingam Perumal Sep 09 '16 at 14:30
  • What is the result if you `SELECT * FROM user_detail WHERE user_age_list IN(1,22,34)`;? It seems to me the problem may be how the data is structured in that table then... – xQbert Sep 09 '16 at 14:34
  • I am getting this result: `1,22,34` – Ramalingam Perumal Sep 09 '16 at 14:42
  • Then the where clause of `WHERE ID = '1'` is causing no records to be returned from the subquery. Why do you need ID=1 in the user_age_list? At this point I would need to see this behavior as a SQLFiddle.com. I can't imange why this or @JayDipJ 's solution is not working – xQbert Sep 09 '16 at 14:44