Request from two tables with SUM

XTRUST.ORG

I have a MySQL request:

SELECT U.*, SUM(P.cost) AS spent FROM `#__users` AS U LEFT JOIN `#__projects` AS P ON P.client_id = U.id WHERE 1=1 AND (spent >= 1000 AND spent < 2000) GROUP BY U.id ORDER BY U.user_creation_timestamp DESC LIMIT 25 OFFSET 0;

But I have error here:

Column not found: 1054 Unknown column 'spent' in 'where clause'

How can I fix the issue? Thanks!

scaisEdge

You can't use alias in where and you can't filter aggregation function with where for filter aggregation function you must use having

SELECT 
    U.*, 
    SUM(P.cost) AS spent 
FROM `#__users` AS U 
LEFT JOIN `#__projects` AS P ON P.client_id = U.id 
GROUP BY U.id 
HAVING  (SUM(P.cost)  >= 1000 AND SUM(P.cost)  < 2000) 
ORDER BY U.user_creation_timestamp DESC LIMIT 25 OFFSET 0;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Request from two tables with SUM

From Dev

sql sum from two tables

From Dev

Sum of two columns from two tables - MySQL

From Dev

Mysql Select with two SUM from two tables

From Dev

Sum of two columns from two tables

From Dev

SQL sum and group values from two tables

From Dev

Php, MySql Sum from two tables

From Dev

SQL SELECT SUM from two tables and group by

From Dev

MYSQL sum from two different tables group by

From Dev

Query to sum from two different tables

From Dev

Query to retrieve sum of values from two tables

From Dev

mysql | Request with checking by time from two tables

From Dev

mysql query request from two tables with IN clause

From Dev

mysql query request from two tables with IN clause

From Dev

How can I sum two rows from two different tables?

From Dev

Sum two columns from two tables without cartesian

From Dev

Compare two columns from two tables and return SUM of matches

From Dev

JOINING two tables to fetch COUNT from one and SUM from the other

From Dev

Summing the SUM of two tables

From Dev

SQL: How to to SUM two values from different tables

From Dev

Getting the sum of several columns from two tables result is not correct

From Dev

Sum data from two tables with different number of rows

From Dev

How can I sum two fileds from different tables in PIG?

From Dev

Sum data from two tables with different number of rows

From Dev

How to sum columns from two tables if the month match and group by month

From Dev

SUM from Multiple tables?

From Dev

MySQL - Group By SUM with two tables

From Dev

Joining two tables with a Sum condition

From Dev

sql sum count(*) of two tables

Related Related

HotTag

Archive