开发者

Creating a Variable that is the Sum of Two Other Variables

For the code below, I would like to make a new variable called totalScore2 that equals days + totalScore.

How can I do this?

Thanks in advance,

John

$sqlStr = "SELECT 
    l.loginid, 
    l.username, 
    l.created,
    DATEDIFF(NOW(), l.created) AS days,
    COALESCE(s.total, 0) AS countSubmissions, 
    COALESCE(c.t开发者_JAVA百科otal, 0) AS countComments,
    COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore


SELECT DATEDIFF(NOW(), l.created) + COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore2

The takeaway from this is that in SQL, you can't reference other columns from the same SELECT statement directly; instead, you have to specify the entire formula. Or, you can use a subquery, but that usually just makes a mountain out of a molehill.

Of course this will make a new column, not a new variable, but I'm pretty sure that's what you were after.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜