1

What is wrong with my "where in" clause in mysql?

My two tables SEATS and REGISTERS look like this

--    SEATS                          REGISTERS
| seat_id (int) |     | register_id (int)| seat_id (varchar) |
|===============|     |==================|===================|
|     102       |     |      3           |   102,103,104     |
|     103       |     |    234           |   546,547         |

The query to fetch the matching results is

SELECT * FROM Seats s, Registers r 
WHERE s.seat_id IN (r.seat_ids) 
GROUP BY s.seat_id

Can someone figure out what's wrong ? Thanks,

2 Answers 2

1

IN requires the list to be a literal list, not a comma-delimited string. Use FIND_IN_SET for that:

WHERE FIND_IN_SET(s.seat_id, r.seat_ids)
Sign up to request clarification or add additional context in comments.

2 Comments

wow, this really works :O never heard of find_in_set before! Going to save it forever! hehe :) many thanks
It's right there on the documentation page of string functions. dev.mysql.com/doc/refman/5.1/en/string-functions.html
0

Firstly change the seat_ids to int, else maintain a separate table where the linkage of seats and registers are maintained.

Join like below can help club results of 2 tables

SELECT * FROM Seats s
JOIN Registers r ON s.seat_id = r.seat_ids

But this will require same data type of Seats seat_id & Registers seat_ids

Else what @Barmar said can be used

Comments

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.