doufang6268 2010-07-31 22:58
浏览 22
已采纳

SQL / PHP:按不同表格的最高投票排序

I needed to recommend to the visitor similar items to the one they where looking at. So I asked in another question for a SQL query to get items with the same categories (from a 'categories' table). The next is the correct code I'm actually using:

SELECT c2.itemID
FROM categories c1
JOIN categories c2 ON c1.catID = c2.catID
WHERE c1.itemID = :id
AND c2.itemID <> :id
GROUP BY c2.itemID
ORDER BY count(c2.itemID) DESC;

It returns items ordered by the number of categories matches (from a table containing 'itemIDs' and 'catIDs'). So for example: item 1 have categories 2,3,4,5,6 and item 2 have categories 2,3,4,5,6 and item 3 have categories 3,5,6 then if I compare item 1 (the one the visitor is looking at) to item 2 and 3 I need to get item 2 first and then item 3 because item 2 have more categories matches than item 3.

NOW: I need also to to order the results by highest vote. The items votes are in a different table. The table 'votes' contain two columns: 'itemID' and 'total_value', 'total_value' being the final rate.

So, How to order the results also by the number of categories matches and also by highest vote from a different table???

Update: The number if items in the table is 8000+. I think the better thing to do to recommend the most similar items is to order it first by items with the exact set of categories and then by votes. Any ideas? Thanks!

  • 写回答

1条回答 默认 最新

  • doujiyan0031 2010-07-31 23:08
    关注
    SELECT c2.itemID
    FROM categories c1
    JOIN categories c2 ON c1.catID = c2.catID
    JOIN votes v ON v.itemID=c1.itemId
    WHERE c1.itemID = :id
    AND c2.itemID <> :id
    GROUP BY c2.itemID
    ORDER BY count(c2.itemID), v.total_value DESC;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口