MySQL 根据找到的 ID 返回 1 或 0
MySQL Returning 1 or 0 based on ID found
这是相关表格的片段:
用户
| 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)
我有以下查询,其中 returns 事件具有票数和基于此的排名:
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
我还想 return 1 或 0 如果用户没有资格投票。
如果出现以下情况,用户将没有资格投票:
用户是活动的所有者。
用户已经为该活动投票(用户 uid 和活动 uid 在 event_vote 中找到)。
以下代码在 ...AS 投票 之后给了我正确的响应,如果用户已经投票但如果他们拥有该事件则不会。
,(
SELECT COUNT(*)
FROM event_vote ev
WHERE ev.event_uid = e.guid
AND ev.user_uid = '{$user_uid}'
) AS ineligible
预期结果(查看所有事件时从用户 2 的角度来看)
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).
您需要检查 User_UID 是否存在 User_Event table 中的投票事件。如果是,投票者就是所有者。
类似于:(伪装我不在编译器附近)
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 语句中。
您可以将此查询与给出 1 或 null
的 case when
表达式一起使用。这被聚合为 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
,但不能保证。
这是相关表格的片段:
用户
| 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)
我有以下查询,其中 returns 事件具有票数和基于此的排名:
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
我还想 return 1 或 0 如果用户没有资格投票。
如果出现以下情况,用户将没有资格投票:
用户是活动的所有者。
用户已经为该活动投票(用户 uid 和活动 uid 在 event_vote 中找到)。
以下代码在 ...AS 投票 之后给了我正确的响应,如果用户已经投票但如果他们拥有该事件则不会。
,(
SELECT COUNT(*)
FROM event_vote ev
WHERE ev.event_uid = e.guid
AND ev.user_uid = '{$user_uid}'
) AS ineligible
预期结果(查看所有事件时从用户 2 的角度来看)
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).
您需要检查 User_UID 是否存在 User_Event table 中的投票事件。如果是,投票者就是所有者。
类似于:(伪装我不在编译器附近)
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 语句中。
您可以将此查询与给出 1 或 null
的 case when
表达式一起使用。这被聚合为 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
,但不能保证。