Wilf
Wilf

Reputation: 2315

How to compare value from sum() with mysql?

I want to compare the value from sum() which I declared as tt_buy. But when I compare at the end. I got an error :

Fatal error: Uncaught mysqli_sql_exception: Unknown column 'tt_buy' in 'where clause'

Here is my code :

select *,sum(b.buy) tt_buy from huay_today_bet b
    inner join huay_today t on t.hid=b.hid
    inner join settings_price_level l on l.rid=t.sid
    where b.hid=? and b.pmode=? and b.num=? and l.lv_bet<tt_buy

I know there anything wrong with my comparison. But I don't know what to search with.

Upvotes: 0

Views: 340

Answers (1)

Rahul Biswas
Rahul Biswas

Reputation: 3467

In your query tt_buy was used as alias. Here I use subquery to avoid error. In MySQL everything is possible so GROUP BY isn't used. But need to use GROUP BY for proper writting query. Different alias name uses if multiple columns with same name.

-- MySQL 
SELECT t.*
FROM (select *,sum(b.buy) tt_buy from huay_today_bet b
    inner join huay_today t on t.hid=b.hid
    inner join settings_price_level l on l.rid=t.sid
    where b.hid=? and b.pmode=? and b.num=? ) t
WHERE t.lv_bet<t.tt_buy

Upvotes: 1

Related Questions