Mysql subquery difference error

lovemysql

Hey guys i am new to mysql actually ..I have wrote two types of code,

first type

select ID,
(select n.NAME from CUSTOMERS as n group by SALARY ) as m
from CUSTOMERS;

second type

select ID,
from (select n.NAME from CUSTOMERS as n) as m
group by SALARY;

The first one works fine as i expected but the second one didnt ..It throws me error like

Unknown column 'ID' in 'field list': select ID from (select NAME from CUSTOMERS ) as m group by SALARY

So by question is can the second way be done in mysql without any errors actually ??

Hope you guys can help me out..Thx

JNevill

Your second one fails because you request column ID from a subquery that does not have ID:

select ID,
from (select n.NAME from CUSTOMERS as n) as m
group by SALARY;

It only has Name. You will need to add both ID and SALARY to your subquery if you wish to use them in your main query.

select ID,
from (select n.ID, n.SALARY from CUSTOMERS as n) as m
group by SALARY;

Also as @Strawyberry pointed out... it's nonsense. You are grouping by a field that isn't in your SELECT clause which... I don't know. You are also subquerying on something that doesn't need to be subqueried. Just do:

select m.ID,
from CUSTOMERS as m
group by m.SALARY;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL syntax error in subquery

From Dev

mySQL Error 1064 in subquery

From Dev

Mysql subquery error 1241

From Dev

mysql subquery with multiple results error

From Dev

Mysql update thows error in subquery

From Dev

mySQL gives syntax error on subquery with valid syntax

From Dev

MySQL reference outer table alias in subquery error

From Dev

Mysql error with time range from subquery

From Dev

MySql subquery runs instead of returning error

From Dev

MySQL subquery as alias - unknown column error

From Dev

Error in this subquery

From Dev

MySQL subquery error ERROR 1242 (21000): Subquery returns more than 1 row

From Dev

MySQL Duplicate column error only when query wrapped as subquery

From Dev

MySQL Subquery error: SQLSTATE[42S21]:

From Dev

MySQL Error Message: Subquery Returns More than 1 Row

From Dev

UPDATE MySQL query error "Subquery returns more than 1 row"

From Dev

MYSQL Error - Subquery returns more than 1 row

From Dev

MySQL: Error code : #1242 - Subquery returns more than 1 row

From Dev

MySQL Duplicate column error only when query wrapped as subquery

From Dev

Error Code: 1242 Subquery returns more than 1 row mysql

From Dev

MYSQL - Error: #1242 - Subquery returns more than 1 row

From Dev

UPDATE MySQL query error "Subquery returns more than 1 row"

From Dev

Difference between ON and WHERE in subquery

From Dev

Difference visibility in subquery join and where

From Dev

Difference visibility in subquery join and where

From Dev

Difference between two dates with subquery

From Dev

MySQL subquery JOIN

From Dev

Very slow MySQL subquery

From Dev

Transform on MySql subquery