Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting Unknown column in field list?

When I run this command, I get the Unknown column 'Orders' in 'field list'

I am trying to get customer list with number of orders, and a new column for type. If the customer has more than 10 orders. It is a big buyer etc.

 SELECT 
    customerTable.isActive,
    (SELECT 
            COUNT(*)
        FROM
            orderTable
        WHERE
            orderTable.customerId = customerTable.id) AS Orders,
    
    CASE
        WHEN Orders > 10 THEN 'Big buyer'
        WHEN Orders > 12 THEN 'Biggest buyer'
    END AS 'Type'
   
    FROM customerTable

Also what is the correct term when you use select in the column section of my query?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think this will work for you.

SELECT 
    customerTable.isActive,
    @Orders := (SELECT COUNT(*)
                FROM orderTable
                WHERE orderTable.customerId = customerTable.id) AS Orders,
    CASE
        WHEN @Orders > 10 THEN 'Big buyer'
        WHEN @Orders > 12 THEN 'Biggest buyer'
    END AS 'Type'
FROM customerTable;

The SELECT in the column list is just considered a subquery.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading