Domanda

Trying to pull this query off, but it won't take ?

SELECT cute_news.title, cute_news.id, cute_news.keywords, cute_fields.post_id, cute_fields.alternate_url 
FROM cute_news, cute_fields 
WHERE cute_news.id = cute_fields.post_id AND 
FIND_IN_SET ('37',cute_news.category) AND 
FIND_IN_SET ('45',cute_news.keywords)

Not sure if I can't have two instances of FIND_IN_SET or what ?

È stato utile?

Soluzione

Do both these queries return some common results?

Query 1

SELECT cute_news.title, cute_news.id, cute_news.keywords, cute_fields.post_id, cute_fields.alternate_url 
FROM cute_news, cute_fields 
WHERE cute_news.id = cute_fields.post_id AND 
FIND_IN_SET ('37',cute_news.category)

Query 2

SELECT cute_news.title, cute_news.id, cute_news.keywords, cute_fields.post_id, cute_fields.alternate_url 
FROM cute_news, cute_fields 
WHERE cute_news.id = cute_fields.post_id AND 
FIND_IN_SET ('45',cute_news.keywords)

Your query should return the intersection of Query 1 and Query 2. If either result set is empty, or if they have no results in common, then the final query will have no results.

Altri suggerimenti

FIND_IN_SET returns the index in the comma-separated string, from 1 to N, or 0 if not found. The arguments are FIND_IN_SET(needle, haystack).

I am assuming that cute_news.category and cute_news.keywords contain a comma-separated list of items of which "37" and "45" may be a valid value in each list?

i.e. FIND_IN_SET ('37',cute_news.category) where cute_news.category for a matching row might contain the value "25,45,37,19". The match starts at position 7.

As written, you are searching for category not containing 37 and keywords not containing 45.

What you want is:

SELECT cute_news.title, cute_news.id, cute_news.keywords, cute_fields.post_id, cute_fields.alternate_url 
FROM cute_news, cute_fields 
WHERE cute_news.id = cute_fields.post_id AND 
FIND_IN_SET ('37',cute_news.category) > 0 AND 
FIND_IN_SET ('45',cute_news.keywords) > 0;

By the way, you can write a stored proc with the "IN" list as a parameter using FIND_IN_SET instead of IN (which cannot be parameterized) if what you want is to select any rows matching multiple possible categories.

DELIMITER $
DROP PROCEDURE IF EXISTS sp_GetAllPostsByCategoriesWithKeyword$
CREATE PROCEDURE sp_GetAllPostsByCategoriesWithKeyword(
    in $category_list VARCHAR(1000),
    in $keyword VARCHAR(100)
  )
    BEGIN
    SELECT cute_news.title, cute_news.id, cute_news.keywords, cute_fields.post_id, cute_fields.alternate_url 
    FROM cute_news, cute_fields
    WHERE cute_news.id = cute_fields.post_id 
       AND FIND_IN_SET (cute_news.category, $category_list) > 0
       AND FIND_IN_SET ($keyword, cute_news.keywords) > 0
    END $
DELIMITER ;

The above assumes that the cute_news.category field contains a single category (ex: "37") in the Db row and $category_list is the list of categories you wish to accept ("12,24,37,51"). The next term is the opposite. You only want to find keyword = "45" in a field that contains a list of keywords ("1,22,45"). FIND_IN_SET only works when you have a single item for the needle.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top