Mysql query, multiple where conditions, which field matches?
given a mysql query in the form like this:
SELECT *
FROM table
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
Which is the best way to tag the "matching fields" ?
For example:
+----+--------+--------+----------+
| ID | field1 | field2 | field3 |
+----+--------+--------+----------+
| 01 | foo | xxx | xxx |
+----+--------+--------+----------+
| 02 | xxx |12bar21 |xxfoobarxx|
+----+--------+--------+----------+
| 03 | f2o | bar | yxz |
+----+--------+--------+----------+
Here every record would be matched - now I want to know which field for each record matches. (So for ID 01 it is field1, for ID 02 it is field2, field3 and for ID 03 it is field2)
Thank you
mysql sql
add a comment |
given a mysql query in the form like this:
SELECT *
FROM table
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
Which is the best way to tag the "matching fields" ?
For example:
+----+--------+--------+----------+
| ID | field1 | field2 | field3 |
+----+--------+--------+----------+
| 01 | foo | xxx | xxx |
+----+--------+--------+----------+
| 02 | xxx |12bar21 |xxfoobarxx|
+----+--------+--------+----------+
| 03 | f2o | bar | yxz |
+----+--------+--------+----------+
Here every record would be matched - now I want to know which field for each record matches. (So for ID 01 it is field1, for ID 02 it is field2, field3 and for ID 03 it is field2)
Thank you
mysql sql
add a comment |
given a mysql query in the form like this:
SELECT *
FROM table
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
Which is the best way to tag the "matching fields" ?
For example:
+----+--------+--------+----------+
| ID | field1 | field2 | field3 |
+----+--------+--------+----------+
| 01 | foo | xxx | xxx |
+----+--------+--------+----------+
| 02 | xxx |12bar21 |xxfoobarxx|
+----+--------+--------+----------+
| 03 | f2o | bar | yxz |
+----+--------+--------+----------+
Here every record would be matched - now I want to know which field for each record matches. (So for ID 01 it is field1, for ID 02 it is field2, field3 and for ID 03 it is field2)
Thank you
mysql sql
given a mysql query in the form like this:
SELECT *
FROM table
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
Which is the best way to tag the "matching fields" ?
For example:
+----+--------+--------+----------+
| ID | field1 | field2 | field3 |
+----+--------+--------+----------+
| 01 | foo | xxx | xxx |
+----+--------+--------+----------+
| 02 | xxx |12bar21 |xxfoobarxx|
+----+--------+--------+----------+
| 03 | f2o | bar | yxz |
+----+--------+--------+----------+
Here every record would be matched - now I want to know which field for each record matches. (So for ID 01 it is field1, for ID 02 it is field2, field3 and for ID 03 it is field2)
Thank you
mysql sql
mysql sql
edited Nov 20 '18 at 4:57
billynoah
10.8k64364
10.8k64364
asked Nov 20 '18 at 4:43
Simon AschemeierSimon Aschemeier
185
185
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can add flags using conditionals to your select like:
SELECT *,
IF(field1 LIKE '%foo%', 1, 0) AS field1_matches,
IF(field2 LIKE '%bar%', 1, 0) AS field2_matches,
IF(field3 LIKE '%foobar%', 1, 0) AS field3_matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
If you need the results in a single field as a comma separated list you can concatenate the results using CONCAT_WS
and return NULL
inside your conditional when there's no match:
SELECT *,
CONCAT_WS(',',
IF(field1 LIKE '%foo%', 'field1', NULL),
IF(field2 LIKE '%bar%', 'field2', NULL),
IF(field3 LIKE '%foobar%', 'field3', NULL)
) AS matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
Thank you, this works nice. Of course all the other approaches work as well, but yours is the easiest
– Simon Aschemeier
Nov 27 '18 at 7:09
add a comment |
How about a CASE
expression:
SELECT
CASE WHEN field1 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
Demo
If you just want to match substrings, then use this:
SELECT
CASE WHEN field1 REGEXP 'foo|bar|foobar'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP 'foo|bar|foobar'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP 'foo|bar|foobar'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
add a comment |
Using CONCAT
and CASE
You are exactly looking for the below result but I know you wish to separate fields with comma or any other sign, for this I can't access mysql
now so I think you can manage that by yourself
SELECT *,
CONCAT(CASE WHEN field1 LIKE '%foo%' THEN 'field1' ELSE NULL END,
CASE WHEN field2 LIKE '%bar%' THEN 'field2' ELSE NULL END,
CASE WHEN field3 LIKE '%foobar%' THEN 'field3' ELSE NULL END) AS macthed_column
FROM youtable
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
OUTPUT:
ID field1 field2 field3 macthed_column
1 foo xxx xxx field1
2 xxx 12bar21 xxfoobarxx field2field3
3 f2o bar yxz field2
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386368%2fmysql-query-multiple-where-conditions-which-field-matches%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can add flags using conditionals to your select like:
SELECT *,
IF(field1 LIKE '%foo%', 1, 0) AS field1_matches,
IF(field2 LIKE '%bar%', 1, 0) AS field2_matches,
IF(field3 LIKE '%foobar%', 1, 0) AS field3_matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
If you need the results in a single field as a comma separated list you can concatenate the results using CONCAT_WS
and return NULL
inside your conditional when there's no match:
SELECT *,
CONCAT_WS(',',
IF(field1 LIKE '%foo%', 'field1', NULL),
IF(field2 LIKE '%bar%', 'field2', NULL),
IF(field3 LIKE '%foobar%', 'field3', NULL)
) AS matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
Thank you, this works nice. Of course all the other approaches work as well, but yours is the easiest
– Simon Aschemeier
Nov 27 '18 at 7:09
add a comment |
You can add flags using conditionals to your select like:
SELECT *,
IF(field1 LIKE '%foo%', 1, 0) AS field1_matches,
IF(field2 LIKE '%bar%', 1, 0) AS field2_matches,
IF(field3 LIKE '%foobar%', 1, 0) AS field3_matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
If you need the results in a single field as a comma separated list you can concatenate the results using CONCAT_WS
and return NULL
inside your conditional when there's no match:
SELECT *,
CONCAT_WS(',',
IF(field1 LIKE '%foo%', 'field1', NULL),
IF(field2 LIKE '%bar%', 'field2', NULL),
IF(field3 LIKE '%foobar%', 'field3', NULL)
) AS matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
Thank you, this works nice. Of course all the other approaches work as well, but yours is the easiest
– Simon Aschemeier
Nov 27 '18 at 7:09
add a comment |
You can add flags using conditionals to your select like:
SELECT *,
IF(field1 LIKE '%foo%', 1, 0) AS field1_matches,
IF(field2 LIKE '%bar%', 1, 0) AS field2_matches,
IF(field3 LIKE '%foobar%', 1, 0) AS field3_matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
If you need the results in a single field as a comma separated list you can concatenate the results using CONCAT_WS
and return NULL
inside your conditional when there's no match:
SELECT *,
CONCAT_WS(',',
IF(field1 LIKE '%foo%', 'field1', NULL),
IF(field2 LIKE '%bar%', 'field2', NULL),
IF(field3 LIKE '%foobar%', 'field3', NULL)
) AS matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
You can add flags using conditionals to your select like:
SELECT *,
IF(field1 LIKE '%foo%', 1, 0) AS field1_matches,
IF(field2 LIKE '%bar%', 1, 0) AS field2_matches,
IF(field3 LIKE '%foobar%', 1, 0) AS field3_matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
If you need the results in a single field as a comma separated list you can concatenate the results using CONCAT_WS
and return NULL
inside your conditional when there's no match:
SELECT *,
CONCAT_WS(',',
IF(field1 LIKE '%foo%', 'field1', NULL),
IF(field2 LIKE '%bar%', 'field2', NULL),
IF(field3 LIKE '%foobar%', 'field3', NULL)
) AS matches
FROM TABLE
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
edited Nov 20 '18 at 5:15
answered Nov 20 '18 at 4:51
billynoahbillynoah
10.8k64364
10.8k64364
Thank you, this works nice. Of course all the other approaches work as well, but yours is the easiest
– Simon Aschemeier
Nov 27 '18 at 7:09
add a comment |
Thank you, this works nice. Of course all the other approaches work as well, but yours is the easiest
– Simon Aschemeier
Nov 27 '18 at 7:09
Thank you, this works nice. Of course all the other approaches work as well, but yours is the easiest
– Simon Aschemeier
Nov 27 '18 at 7:09
Thank you, this works nice. Of course all the other approaches work as well, but yours is the easiest
– Simon Aschemeier
Nov 27 '18 at 7:09
add a comment |
How about a CASE
expression:
SELECT
CASE WHEN field1 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
Demo
If you just want to match substrings, then use this:
SELECT
CASE WHEN field1 REGEXP 'foo|bar|foobar'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP 'foo|bar|foobar'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP 'foo|bar|foobar'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
add a comment |
How about a CASE
expression:
SELECT
CASE WHEN field1 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
Demo
If you just want to match substrings, then use this:
SELECT
CASE WHEN field1 REGEXP 'foo|bar|foobar'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP 'foo|bar|foobar'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP 'foo|bar|foobar'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
add a comment |
How about a CASE
expression:
SELECT
CASE WHEN field1 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
Demo
If you just want to match substrings, then use this:
SELECT
CASE WHEN field1 REGEXP 'foo|bar|foobar'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP 'foo|bar|foobar'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP 'foo|bar|foobar'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
How about a CASE
expression:
SELECT
CASE WHEN field1 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP '[[:<:]]foo[[:>:]]|[[:<:]]bar[[:>:]]|[[:<:]]foobar[[:>:]]'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
Demo
If you just want to match substrings, then use this:
SELECT
CASE WHEN field1 REGEXP 'foo|bar|foobar'
THEN 'field1' ELSE '' END AS field1match,
CASE WHEN field2 REGEXP 'foo|bar|foobar'
THEN 'field2' ELSE '' END AS field2match,
CASE WHEN field3 REGEXP 'foo|bar|foobar'
THEN 'field3' ELSE '' END AS field3match
FROM yourTable
edited Nov 20 '18 at 5:14
answered Nov 20 '18 at 4:48
Tim BiegeleisenTim Biegeleisen
226k1392146
226k1392146
add a comment |
add a comment |
Using CONCAT
and CASE
You are exactly looking for the below result but I know you wish to separate fields with comma or any other sign, for this I can't access mysql
now so I think you can manage that by yourself
SELECT *,
CONCAT(CASE WHEN field1 LIKE '%foo%' THEN 'field1' ELSE NULL END,
CASE WHEN field2 LIKE '%bar%' THEN 'field2' ELSE NULL END,
CASE WHEN field3 LIKE '%foobar%' THEN 'field3' ELSE NULL END) AS macthed_column
FROM youtable
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
OUTPUT:
ID field1 field2 field3 macthed_column
1 foo xxx xxx field1
2 xxx 12bar21 xxfoobarxx field2field3
3 f2o bar yxz field2
add a comment |
Using CONCAT
and CASE
You are exactly looking for the below result but I know you wish to separate fields with comma or any other sign, for this I can't access mysql
now so I think you can manage that by yourself
SELECT *,
CONCAT(CASE WHEN field1 LIKE '%foo%' THEN 'field1' ELSE NULL END,
CASE WHEN field2 LIKE '%bar%' THEN 'field2' ELSE NULL END,
CASE WHEN field3 LIKE '%foobar%' THEN 'field3' ELSE NULL END) AS macthed_column
FROM youtable
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
OUTPUT:
ID field1 field2 field3 macthed_column
1 foo xxx xxx field1
2 xxx 12bar21 xxfoobarxx field2field3
3 f2o bar yxz field2
add a comment |
Using CONCAT
and CASE
You are exactly looking for the below result but I know you wish to separate fields with comma or any other sign, for this I can't access mysql
now so I think you can manage that by yourself
SELECT *,
CONCAT(CASE WHEN field1 LIKE '%foo%' THEN 'field1' ELSE NULL END,
CASE WHEN field2 LIKE '%bar%' THEN 'field2' ELSE NULL END,
CASE WHEN field3 LIKE '%foobar%' THEN 'field3' ELSE NULL END) AS macthed_column
FROM youtable
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
OUTPUT:
ID field1 field2 field3 macthed_column
1 foo xxx xxx field1
2 xxx 12bar21 xxfoobarxx field2field3
3 f2o bar yxz field2
Using CONCAT
and CASE
You are exactly looking for the below result but I know you wish to separate fields with comma or any other sign, for this I can't access mysql
now so I think you can manage that by yourself
SELECT *,
CONCAT(CASE WHEN field1 LIKE '%foo%' THEN 'field1' ELSE NULL END,
CASE WHEN field2 LIKE '%bar%' THEN 'field2' ELSE NULL END,
CASE WHEN field3 LIKE '%foobar%' THEN 'field3' ELSE NULL END) AS macthed_column
FROM youtable
WHERE field1 LIKE '%foo%'
OR field2 LIKE '%bar%'
OR field3 LIKE '%foobar%'
OUTPUT:
ID field1 field2 field3 macthed_column
1 foo xxx xxx field1
2 xxx 12bar21 xxfoobarxx field2field3
3 f2o bar yxz field2
edited Nov 20 '18 at 6:00
answered Nov 20 '18 at 5:40
SusangSusang
4,6912724
4,6912724
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386368%2fmysql-query-multiple-where-conditions-which-field-matches%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown