Слева присоединиться к пользователям дважды и включить исключение ID 2 в предложении ON
. Получите изображение профиля, которое не NULL
, используя coalesce()
.
SELECT g.games_id,
coalesce(u1.profile_pic,
u2.profile_pic) profile_pic
FROM games g
LEFT JOIN users u1
ON u1.users_id = g.player_1_users_id
AND g.player_1_users_id <> 2
LEFT JOIN users u2
ON u2.users_id = g.player_2_users_id
AND g.player_2_users_id <> 2
WHERE 2 IN (g.player_1_users_id,
g.player_2_users_id)
AND g.status = 3;
Sure, just convert to a number of seconds (minutes, milliseconds, hours, take your pick of units) and do the division.
EDIT (again): so you can't assign to timedelta.__div__
. Try this, then:
divtdi = datetime.timedelta.__div__
def divtd(td1, td2):
if isinstance(td2, (int, long)):
return divtdi(td1, td2)
us1 = td1.microseconds + 1000000 * (td1.seconds + 86400 * td1.days)
us2 = td2.microseconds + 1000000 * (td2.seconds + 86400 * td2.days)
return us1 / us2 # this does integer division, use float(us1) / us2 for fp division
And to incorporate this into nadia's suggestion:
class MyTimeDelta:
__div__ = divtd
Example usage:
>>> divtd(datetime.timedelta(hours = 12), datetime.timedelta(hours = 2))
6
>>> divtd(datetime.timedelta(hours = 12), 2)
datetime.timedelta(0, 21600)
>>> MyTimeDelta(hours = 12) / MyTimeDelta(hours = 2)
6
etc. Of course you could even name (or alias) your custom class timedelta
so it gets used in place of the real timedelta
, at least in your code.
Деление и умножение на целые числа, кажется, работают из коробки :
>>> from datetime import timedelta
>>> timedelta(hours=6)
datetime.timedelta(0, 21600)
>>> timedelta(hours=6) / 2
datetime.timedelta(0, 10800)
Оператор деления можно переопределить следующим образом:
class MyTimeDelta(timedelta):
def __div__(self, value):
# Dome something about the object