This request does not work. Why?

Select id,name_column as name from test_table where name='test' 

Throws an "invalid identifier" error. those. if the request is of the form:

 Select id,(select count(*) from test_table)as tmp_col from test_table where tmp_col>1 

then it will not work either. How to win?

    2 answers 2

    Because in WHERE you cannot use the ALIAS name_column AS name . You need to write name_column = 'test' .

    • Ok, then how to be with Select id, (select count (*) from test_table) as tmp_col from test_table where tmp_col> 1 How to get count? - CrazyTimon
    • So: Select id, (select count ( ) from test_table) as tmp_col from test_table where (select count ( ) from test_table)> 1 - renegator
    • It seems to me that this is correct: Select id, tmp_col from (Select id, (select count () from test_table) as tmp_col from test_table) as outerTable where tmpl_col> 1 - null

    If I understand correctly, you need to get the id from the table, if it has more than one record (at least without grouping it looks that way). In this case, it is more correct to use such code.

     select x.id from test_table x where exists (select 1 from test_table y where y.id != x.id ); 

    Using count (*) to find out if something is there or not is wrong. It is better to immediately do what you need, and, if necessary, handle the exception.