doujing1967 2015-01-29 21:01
浏览 43
已采纳

MySQL:通过“分组依据”将搜索功能搜索到聚合列中

I've got 3 tables: members, photos and marks. Members can post pictures and rank the pictures of the other members.

I'm doing a script in PHP which returns a photo (using random()) that can be ranked by the user connected in his member area. Thus, I made the following query (I commented the line which obviously contain a problem):

 SELECT ph.*, 
    COUNT(note) 'nb_note', 
    ROUND(DEGREES(ACOS((SIN(RADIANS( 48.86 )) * SIN (RADIANS( v.latitude_deg ))) + (COS(RADIANS( 48.86 )) * COS(RADIANS( v.latitude_deg )) * COS( RADIANS( 2.34445 - v.longitude_deg))))) * 111.13384) 'distance', 

    ((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(mb.anniv)) / 3600 / 24 / 365) AS 'age' , mb.sexe, pr.orientation, mb.pseudo, mb.anniv,mb.ID 'ID_membre' 

    FROM photos__ ph 

    LEFT JOIN photos__rank rk ON rk.ID_photo = ph.ID 
    LEFT JOIN photos__signalements sg ON sg.ID_photo = ph.ID 
    INNER JOIN membre__ mb ON mb.ID = ph.ID_membre 
    INNER JOIN membre__profil pr ON pr.ID_membre = mb.ID 
    INNER JOIN site__villes v ON v.ID = pr.ID_ville 

    // '96' is the currently connected member, written into the query by PHP
    WHERE mb.ID <> '96'
// THE FOLLOWING LINE SHOULD PREVENT THE QUERY TO RETURN PICTURES ALREADY RANKED // BY THE USER CURRENTLY CONNECTED, BUT IT DOESN'T WORK :
    AND (rk.ID_membre <> '96' OR ISNULL(note)) // THIS LINE DOESN'T WORK
// SAME PROBLEMS WITH PICTURES ALREADY REPORTED BY THE USER '96' (the connected):    
 AND (sg.ID_membre <> '96' OR ISNULL(sg.ID)) // AND THIS ONE AS WELL
    AND ph.innotable = 0 
    AND mb.sexe = 'f' 
    AND pr.orientation IN ('hetero', 'bi')

    GROUP BY ph.ID HAVING distance < 10 AND age >= 16 

    ORDER BY RAND() LIMIT 1

well, I wrote a "group by" clause to return just the row with the informations I need to display (distance, age, ID of the member, ID of the photo etc) . The problem is, when several members has already ranked the same photo, this query can return photos already ranked by the user. I found it's because when I say "where rk.ID_membre <> '96' OR ISNULL(note)" i say to mysql "you can return a photo if it has no marks yet or if the first mark you find is different from '96'". How to say "you can return if it has no marks or if it has, all the marks must be different from '96').

I need a SQL function as COUNT or AVG which return if an int is or not in the agregated column. I would do sth like

  SELECT .. all the other infos .., 
IS_THERE('96' IN photo) AS 'already_ranked', 
IS_THERE('96' IN signalements) 'already_reported' 
    ..blablabla...
    WHERE/HAVING already_reported = 0 AND already_ranked = 0
    GROUP BY photos.ID

Don't hesitate to tell me if there is another way faster or more simple to make this query.

  • 写回答

1条回答 默认 最新

  • dqrm8199 2015-01-29 21:32
    关注

    Consider:

    SELECT `id`, SUM(CASE WHEN LOCATE('96', `field`) > 0 THEN 1 ELSE 0 END) AS cnt
    FROM `table`
    GROUP BY `id`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码