1

I had an issue of while run a query with IN key return empty. Here's my query:

 SELECT * FROM `sc_miscellaneous` where discount_for=4 AND stud_id IN (1,2) AND status=1

This query returns a correct results. But if i use IN (2,3) or IN (2) means zero results. Eg.

SELECT * FROM `sc_miscellaneous` where discount_for=4 AND stud_id IN (2,3) AND status=1

Table sc_miscellaneous:

ID Miscellaneous misc_amount discount discount_for class stud_id               status
1  5                200        2        4            1    1,2,3,4,5,6,            1  
                                                          7,8,9,10,11, 
                                                          12,13,14,15,16,
                                                          17,18,19 

2  6                500        2        4            1    1,2,3,4,5,6,7           1

Advance Thanks.

2
  • 4
    What is the type of stud_id? Commented Dec 26, 2014 at 6:49
  • refer this or this Commented Dec 26, 2014 at 7:01

3 Answers 3

1

Updated

Use following query

SELECT * FROM `sc_miscellaneous` 
WHERE stud_id LIKE '%,2,%' OR  stud_id LIKE '%,3,%' 
  OR  stud_id LIKE '2,%,' OR  stud_id LIKE '3,%'
  OR  stud_id LIKE '%,2' OR  stud_id LIKE '%,3'

As you are having varchar comma separated list.

Check

1)if that UNIQUE number is in between comma separated list,

2)OR if is that at the start

3)OR if its at the end.

Sign up to request clarification or add additional context in comments.

3 Comments

This will also recover, for example, stud_id = 12 or stud_id = 35. If you're going comma seperated (which you shouldn't) then the comma's need to be included in the LIKE ( '%,2,%' ) and you need to add a comma at the start and end of the value in the column.
but if we use LIKE means, column have '22,32' means its also selected. but we need exact result. -thanks
Thanks Friends, i got it. @Erik and jQuery PHP
0

I don't think the way you are writing your query using IN clause is the right way.

Please follow this link

According to the documentation, if stud_id is an integer then you can check it with multiple integers using IN Clause. Like if stud_id = 1, then you can write stud_id IN ( 1,2,3).

For your requirement you may have to write multiple queries for extracting data and compare.

1 Comment

if change the type of stud_id as int means then how it allows comma. I like to insert all the student id's in a single record instead of insert no. of records. But 1st query is working fine, the issue is in 2nd only, if we select center value(except 1) in stud_id column means no result.
0

I think your stud_id type was int make it to varchar and run the code

stud_id(int)---->change to varchar

you have to work with

in_array()

1 Comment

type of the stud_id is varchar only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.