sql >> Database teknologi >  >> RDS >> Mysql

MySQL-forespørgsel for at finde den mest lignende numeriske række

Som en lille justering af @eggyals metode indarbejdede jeg antallet af elementer, vi er i stand til at matche.

SELECT   u2.user_id

-- join our user to their scores
FROM     (users u1 JOIN scores s1 USING (user_id))

-- and then join other users and their scores
    JOIN (users u2 JOIN scores s2 USING (user_id))
      ON s1.item_id  = s2.item_id
     AND u1.user_id != u2.user_id

-- filter for our user of interest
WHERE    u1.user_id = ?

-- group other users' scores together
GROUP BY u2.user_id

-- subtract the degree of difference in correlating scores from the number of correlating scores
ORDER BY (SUM(s1.item_id = s2.item_id) - 
  ( SUM(ABS(s2.score - s1.score) + ABS(u2.self - u1.self) ) ) ) DESC


  1. Importer .frm- og .opt-filer til MySQL

  2. Mysql for langsom ved simpel forespørgsel mellem to tabeller

  3. Hvad er fordelene ved only_full_group_by mode?

  4. Null reference fra MySQL driver modul fra taskpane.js en Microsoft Office Add-In