MySQL根据找到的ID返回1或0

时间:2018-03-01 11:22:15

标签: mysql sql join count where

以下是相关表格的片段:

用户

|  USER_UID  |  FIRSTNAME  |  LASTNAME  | 
    abc123         bob          smith
    def456         rob          smithies 
    ghi789         john         clark

EVENT

| GUID | NAME |  
  ev1   event1
  ev2   event2
  ev3   event3

USER_EVENT

| USER_EVENT_ID | USER_UID | EVENT_UID | 
       1            abc123     ev1
       2            def456     ev2
       3            ghi789     ev3

EVENT_VOTE

| EVENT_VOTE_ID | USER_UID | EVENT_UID | 
         1         def456      ev1       (user2 voted for user1's event)

我有以下查询返回具有投票数的事件和基于此的排名:

SELECT t.*, @curRank := @curRank + 1 AS rank
FROM ( SELECT e.guid,
              e.name,
              ( SELECT COUNT(ev.event_vote_id) 
                FROM event_vote ev 
                WHERE ev.event_uid = e.guid
              ) AS votes
              FROM event e
      ) AS t
CROSS JOIN (SELECT @curRank := 0) r
ORDER BY votes DESC

如果用户没有资格投票,我也想要返回1或0。

如果符合以下条件,用户将无资格投票:

  • 用户是活动的所有者。

  • 用户已经投票支持该事件(在event_vote中找到了用户uid和事件uid)。

... AS投票之后的以下代码在用户已经投票时给出了正确答案,但如果他们拥有该事件则没有。

,(
    SELECT COUNT(*)
    FROM event_vote ev
    WHERE ev.event_uid = e.guid
    AND ev.user_uid = '{$user_uid}'
 ) AS ineligible

预期结果(查看所有活动时从用户2s的角度来看)

guid: ev1
name: event1
votes: 1 
rank: 1
ineligible: 1 (already voted).

guid: ev2
name: event2
votes: 0
rank: 2
ineligible: 1 (user owns this event)

guid: ev3
name: event3
votes: 0 
rank: 3
ineligible: 0 (user doesn't own this event and has yet to vote).

2 个答案:

答案 0 :(得分:1)

您可以将此查询与case when表达式一起使用,给出1或null。这被聚合为count(distinct ...),仅当聚合值中至少有1时才给出1,否则为0:

SELECT      t.*,
            @curRank := @curRank + 1 AS rank 
FROM        (
                SELECT     u.user_uid,
                           e.guid, 
                           e.name, 
                           count(ev.user_uid) votes, 
                           count(distinct 
                               case when u.user_uid in (ev.user_uid, ue.user_uid)
                                    then 1 
                               end) ineligible 
                FROM       user u
                CROSS JOIN event e
                INNER JOIN user_event ue
                        ON ue.event_uid = e.guid
                LEFT JOIN  event_vote ev
                        ON ev.event_uid = e.guid
                GROUP BY   u.user_uid, 
                           e.guid
                ORDER BY   votes desc,
                           e.guid,
                           u.user_uid
            ) as t
CROSS JOIN (SELECT @curRank := 0) r
WHERE      t.user_uid = 'def456'
ORDER BY   votes desc,
           guid, 
           user_uid

rextester.com上看到它。

请注意,当您使用此类变量时,必须强制执行 inner 查询中的顺序。如果你只在外部查询上执行它,那么实际上已经太晚了,因为已经评估了变量表达式。它可能经常工作,因为优化器可能会考虑外部order by,但不能保证。

答案 1 :(得分:0)

您需要检查User_Event表中的voted事件是否存在User_UID。如果是,选民就是所有者。

之类的:( PSEUDO我不在编译器附近)

     IF EXISTS (Begin
        select User_Event_Id 
        from User_event 
        where User_UId = @user_uid 
        (@user_uid the user trying to vote) 
        and where Event_Id = @Event 
        (@Event = event of the id, the user is trying to vote for)
        End)
        Begin
        if(--Check if user has voted for event (code you already wrote)
        --User is eligible
        else
        --User not eligible
        End

    ELSE
        --User not eligible

但是,最好只花时间合并这两个请求,而不会产生if语句。