开发者

Creating a Variable that is the Sum of Two Other Variables

开发者 https://www.devze.com 2023-01-02 07:12 出处:网络
For the code below, I would like to make a new variable called totalScore2 that equals days + totalScore.

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

精彩评论

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