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

问题描述 投票:0回答:2

以下是相关表格的片段:

用户

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

事件

| 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).
mysql sql join count where
2个回答
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上运行。

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


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语句。

© www.soinside.com 2019 - 2024. All rights reserved.