How to pass in where condition if column have multiple values with comma seperate in mysql [duplicate]
This question already has an answer here:
Is storing a delimited list in a database column really that bad?
10 answers
#id name product_id
1 chalam 1,2
2 satish 2,3
3 siva 3,4
4 gowtham 2,4
select id, name where product_id in(3);
expected output:
satish
siva
mysql
marked as duplicate by Strawberry
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 14:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Is storing a delimited list in a database column really that bad?
10 answers
#id name product_id
1 chalam 1,2
2 satish 2,3
3 siva 3,4
4 gowtham 2,4
select id, name where product_id in(3);
expected output:
satish
siva
mysql
marked as duplicate by Strawberry
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 14:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Can you change the way you store the Ids so you don't use a comma separation? See stackoverflow.com/questions/3653462/…
– Progman
Jan 3 at 14:04
add a comment |
This question already has an answer here:
Is storing a delimited list in a database column really that bad?
10 answers
#id name product_id
1 chalam 1,2
2 satish 2,3
3 siva 3,4
4 gowtham 2,4
select id, name where product_id in(3);
expected output:
satish
siva
mysql
This question already has an answer here:
Is storing a delimited list in a database column really that bad?
10 answers
#id name product_id
1 chalam 1,2
2 satish 2,3
3 siva 3,4
4 gowtham 2,4
select id, name where product_id in(3);
expected output:
satish
siva
This question already has an answer here:
Is storing a delimited list in a database column really that bad?
10 answers
mysql
mysql
edited Jan 3 at 13:41
Tim Biegeleisen
235k13100159
235k13100159
asked Jan 3 at 13:39
Simhachalam SopetiSimhachalam Sopeti
247
247
marked as duplicate by Strawberry
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 14:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Strawberry
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 14:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Can you change the way you store the Ids so you don't use a comma separation? See stackoverflow.com/questions/3653462/…
– Progman
Jan 3 at 14:04
add a comment |
1
Can you change the way you store the Ids so you don't use a comma separation? See stackoverflow.com/questions/3653462/…
– Progman
Jan 3 at 14:04
1
1
Can you change the way you store the Ids so you don't use a comma separation? See stackoverflow.com/questions/3653462/…
– Progman
Jan 3 at 14:04
Can you change the way you store the Ids so you don't use a comma separation? See stackoverflow.com/questions/3653462/…
– Progman
Jan 3 at 14:04
add a comment |
1 Answer
1
active
oldest
votes
You may use FIND_IN_SET
here:
SELECT id, name
FROM yourTable
WHERE FIND_IN_SET('3', product_id) > 0;
Note that while MySQL coincidentally offers FIND_IN_SET
which can search a CSV string for a value, in general we should avoid using this function. Storing CSV data in your tables is bad database design. Instead, you should normalize your data. Here is an example of what your normalized table might look like:
id | name | product_id
1 | chalam | 1
1 | chalam | 2
2 | satish | 2
2 | satish | 3
3 | siva | 3
3 | siva | 4
4 | gowtham | 2
4 | gowtham | 4
Assuming that a given name/id were only associated with a given product_id
once in the above table, you could simplify your query to:
SELECT id, name
FROM yourTable
WHERE product_id = 3;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may use FIND_IN_SET
here:
SELECT id, name
FROM yourTable
WHERE FIND_IN_SET('3', product_id) > 0;
Note that while MySQL coincidentally offers FIND_IN_SET
which can search a CSV string for a value, in general we should avoid using this function. Storing CSV data in your tables is bad database design. Instead, you should normalize your data. Here is an example of what your normalized table might look like:
id | name | product_id
1 | chalam | 1
1 | chalam | 2
2 | satish | 2
2 | satish | 3
3 | siva | 3
3 | siva | 4
4 | gowtham | 2
4 | gowtham | 4
Assuming that a given name/id were only associated with a given product_id
once in the above table, you could simplify your query to:
SELECT id, name
FROM yourTable
WHERE product_id = 3;
add a comment |
You may use FIND_IN_SET
here:
SELECT id, name
FROM yourTable
WHERE FIND_IN_SET('3', product_id) > 0;
Note that while MySQL coincidentally offers FIND_IN_SET
which can search a CSV string for a value, in general we should avoid using this function. Storing CSV data in your tables is bad database design. Instead, you should normalize your data. Here is an example of what your normalized table might look like:
id | name | product_id
1 | chalam | 1
1 | chalam | 2
2 | satish | 2
2 | satish | 3
3 | siva | 3
3 | siva | 4
4 | gowtham | 2
4 | gowtham | 4
Assuming that a given name/id were only associated with a given product_id
once in the above table, you could simplify your query to:
SELECT id, name
FROM yourTable
WHERE product_id = 3;
add a comment |
You may use FIND_IN_SET
here:
SELECT id, name
FROM yourTable
WHERE FIND_IN_SET('3', product_id) > 0;
Note that while MySQL coincidentally offers FIND_IN_SET
which can search a CSV string for a value, in general we should avoid using this function. Storing CSV data in your tables is bad database design. Instead, you should normalize your data. Here is an example of what your normalized table might look like:
id | name | product_id
1 | chalam | 1
1 | chalam | 2
2 | satish | 2
2 | satish | 3
3 | siva | 3
3 | siva | 4
4 | gowtham | 2
4 | gowtham | 4
Assuming that a given name/id were only associated with a given product_id
once in the above table, you could simplify your query to:
SELECT id, name
FROM yourTable
WHERE product_id = 3;
You may use FIND_IN_SET
here:
SELECT id, name
FROM yourTable
WHERE FIND_IN_SET('3', product_id) > 0;
Note that while MySQL coincidentally offers FIND_IN_SET
which can search a CSV string for a value, in general we should avoid using this function. Storing CSV data in your tables is bad database design. Instead, you should normalize your data. Here is an example of what your normalized table might look like:
id | name | product_id
1 | chalam | 1
1 | chalam | 2
2 | satish | 2
2 | satish | 3
3 | siva | 3
3 | siva | 4
4 | gowtham | 2
4 | gowtham | 4
Assuming that a given name/id were only associated with a given product_id
once in the above table, you could simplify your query to:
SELECT id, name
FROM yourTable
WHERE product_id = 3;
answered Jan 3 at 13:41
Tim BiegeleisenTim Biegeleisen
235k13100159
235k13100159
add a comment |
add a comment |
1
Can you change the way you store the Ids so you don't use a comma separation? See stackoverflow.com/questions/3653462/…
– Progman
Jan 3 at 14:04