Domanda

The following query

SELECT ASSOCIATED_RISK 
FROM PROJECT_ISSUES
 WHERE FIND_IN_SET('98',ASSOCIATED_RISK);

returns output as

96,98

90,98

but if I use

SELECT ASSOCIATED_RISK
FROM PROJECT_ISSUES 
 WHERE FIND_IN_SET('96,98',ASSOCIATED_RISK);

it doesn't returns anything.In this case I would like to retrieve the first row.

96,98
È stato utile?

Soluzione 2

Your comment:

is there any other way I can get it in single query instead of framing multiple find_in_set and concat them

As an alternative solution, you can use locate on your ASSOCIATED_RISK value.

Example:

locate( replace( '96,98', ',', '' ), replace( ASSOCIATED_RISK, ',', '' ) )

Edit:
As per Aziz Shaikh comment, we can see that there is a possibility of true result though the search string not existing in the target string.

As an alternative solution, you can replace the search string from target string with an empty string and compare the lengths. If original string's length is grater than new replaced string, then it is a found true result.

Example:

-- this should be greater than 0 for a found true
length( ASSOCIATED_RISK ) > length( replace( ASSOCIATED_RISK, '96,98', '' ) )

Altri suggerimenti

Use the AND clause, like this:

SELECT ASSOCIATED_RISK 
FROM PROJECT_ISSUES 
WHERE FIND_IN_SET('96',ASSOCIATED_RISK)
AND FIND_IN_SET('98',ASSOCIATED_RISK)

Your query is failing because FIND_IN_SET() does not work properly if the first argument contains a comma (",") character. Reference: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set. In your case, the first argument is '96,98', so it fails.

This will Give result. see the difference.

SELECT ASSOCIATED_RISK FROM PROJECT_ISSUES WHERE FIND_IN_SET(ASSOCIATED_RISK,'96,98');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top