sree
sree

Reputation: 498

compare and fetch unique data, stored in comma separation

I have a mysql database where error_code and error_type are stored.

Each Error_type can have multiple error_code stored as comma separation.

Here is a table

id error_code error_type

1 300,200,444 spelling

2 333,310 grammer

As shown ; spelling has 3 error_code

Question: how to query to get error_type if error_code is only 300 or 200 or 444 (only 1 error code will be entered in text box)

let me know if any info needed or if am not clear .... tnx

EDIT:

I need a distinct error_type

Upvotes: 1

Views: 199

Answers (2)

Jainendra
Jainendra

Reputation: 25143

You can use like

select error_type from table where error_code like '%300%'

or

select error_type from table where error_code like '%444%'

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You can use regexp:

select * from test
where error_code REGEXP '(^444,|,444$|,444,|^444$)'

This will ensure that 444 is present either at the beginning, at the end, or in between of two commas; it would not match strings where 444 is inside another number. For example, 1,2,34445,6 will not be matched.

Upvotes: 2

Related Questions