Just use FIND_IN_SET
select t.*,
SIGN(FIND_IN_SET('2',keywords))+SIGN(FIND_IN_SET('4',keywords))
as FindWeight
from t
where
SIGN(FIND_IN_SET('2',keywords))+SIGN(FIND_IN_SET('4',keywords))>0
ORDER BY FindWeight DESC
문제
I am creating a product area.All of the products have some keyword ids in it.What i am going to implement is if a product was selected, i want to display most related other products beneath the page. As for example data,
23 | product1 | 2,4,13
31 | product3 | 2,4,8
10 | product6 | 2,4
11 | product4 | 2,3
34 | product2 | 2,4
54 | product5 | 2,7
12 | product8 | 4,8
Lets say I have to display related products for product contains keyword 2 and 4. Currently I am using following to get the related products
$sql="SELECT * FROM table1 WHERE keywords REGEXP '(^|,)2(,|$)' OR keywords REGEXP '(^|,)4(,|$)' ORDER BY product_id"
But sometimes this will not return mostly related products.
As for the above example data, product_id 23,31,10,34 should take higher precedence rather than other records.
Can that be possible? Then what should i do to overcome this? thanks for all replies.
해결책
Just use FIND_IN_SET
select t.*,
SIGN(FIND_IN_SET('2',keywords))+SIGN(FIND_IN_SET('4',keywords))
as FindWeight
from t
where
SIGN(FIND_IN_SET('2',keywords))+SIGN(FIND_IN_SET('4',keywords))>0
ORDER BY FindWeight DESC
다른 팁
leftclickben is right about how you should have structured your tables.
However if you want to continue with this, then the following would work
keywords LIKE '2,%' OR keyword LIKE '%,2,%' OR keyword LIKE '%,4,%' OR keyword LIKE '%,4,%'
Careful - be very consistent about the placement of commas and spaces.