Alias Select statement in JPA gives invalid column name - sql

I have a problem with these aliased "c" select statements. I get a invalid column "c" error from the database. But all kinds of examples in the web use this type of writing the select statement.
Query query = em.createQuery(
"SELECT c FROM Country c WHERE c.name = 'Canada'");
Country c = (Country)query.getSingleResult();
Is this way of writing vendor specific?
Thanks for your help?

You cannot say just c to get selected because it is just a alias name
You have to try something like this;
select c.name
from country c
where c.name='canada'

When using a SELECT, you specify what columns from the database you want.
If you want all columns, use
SELECT * FROM <table>
or if you are joining tables, and only want columns from that table use
SELECT <table>.* FROM <table>

The above example is a query on the classes/entities and not a query on the native database. C represents the object instance. My mistake was that i mixed up native and normal queries.

Related

AWS Athena Query Structure

I have a complex type and I want to query it using Athena
{id={s=c937b52e-fee8-4899-ae26-d4748e65fb5f}, __typename={s=Account}, role={s=COLLABORATOR}, updatedat={s=2021-04-23T04:38:29.385Z}, entityid={s=70f8a1a8-6f20-4dd3-a484-8385198ddf97}, status={s=ACTIVE}, createdat={s=2021-04-23T04:38:20.045Z}, email={s=dd#mail.com}, showonboarding={bool=true}, position={s=beta}, name={s=User2}, lastlogindate={s=2021-04-23T04:41:07.775Z}}
How to do it?
SELECT c.*
FROM "db"."table" c
LIMIT 10
returns all data in the table. However if I select like
SELECT c.id
FROM "db"."table" c
LIMIT 10
it shows the error.
Thanks in advance.
The query is missing name for column which stores the shown data. You should change your query to something like:
SELECT c.some_column_name.id -- use real column name instead of some_column_name
FROM "db"."table" c
LIMIT 10

Is there a way to get column info on any given query in sql?

My problem is, that I would like to get column names and types etc... not only from one table or a view, but from any select query. For example when joining 2 tables.
If the select query looked like this:
Select * from employees
Then the output it would produce would have such columns as this produces:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employees'
Now I need something like this but it must work with far more complex selects like this one for example.
select e.EmployeeId,e.EmployeeName, et.type, et.IsPermanent
from Employee e
inner join EmployeeType et
on e.EmployeeId = et.EmployeeId
Problem is that this can be any select query and I don’t know what the user will type into my app. I just need a way to get column info from anything.
Ok, so I luckily solved my issue using SqlDataReader:
var columnNames = Enumerable.Range(0,reader.FieldCount).Select(reader.GetName).ToList();
var columnFieldTypes = Enumerable.Range(0, reader.FieldCount).Select(reader.GetFieldType).ToList();

REPLACE with JOIN - SQL

I need help to understand what I did wrong ... I'm a beginner so excuse me the simple question!
I have two tables in which I want to do a JOIN where, in one of the columns I had to use REPLACE to remove the text 'RIxRE' that does not interest me.
In table 1, this is the original text of the column id_notification: RIxRE-1787216-BSB and this is the text that returns when using REPLACE: 1787216-BSB
In column 2, this is the text that exists: 1787216-BSB
However, I get the following error:
# 1054 - Unknown column 'a.id_not' in 'on clause'
SELECT *, REPLACE(a.id_notificacao,'RIxRE','') AS id_not
FROM robo_qualinet_cadastro_remedy a
JOIN (SELECT * FROM painel_monitoracao) b ON a.id_not = b.id_notificacao
You cannot use a column alias again in the FROM clause or the WHERE clause after the SELECT (and possibly not other clauses as well, depending on the database).
So, repeat the expression:
SELECT *, REPLACE(a.id_notificacao, 'RIxRE', '') AS id_not
FROM robo_qualinet_cadastro_remedy rqcr JOIN
painel_monitoracao pm
ON REPLACE(rqcr.id_notificacao, 'RIxRE', '') = pm.id_notificacao;
Notes:
Use table aliases the mean something, such as abbreviations for the able names.
The subquery is not necessary in the FROM clause.
I suspect that you have a problem with your data model if you need a REPLACE() for the JOIN condition, but that is a different issue from this question.

SQL query with regex

I have a table vehicles has a column 'name'
Values are stored like car/tesla, car/honda, truck/daimler (each value is stored as type/brand)
I want to query the table using only brand. If I look up tesla, it should return the row corresponding to car/tesla. How do I do it? I'm using postgres.
There is no need in regex in your case. Just use old good like:
select name
from vehicles
where name like '%/tesla'
2 solutions are available a select query with LIKE operand or a select query with contains operand.
select * from vehicles where name LIKE '%tesla%'
Select * from vehicles where Contains(name, "tesla");

SQL statement for a join in dB2

The following is my sql statement for a join in dB2.
select name, address, bloodgroup
from user_tb, health_tb
where user_tb.id = health_tb.id;
I am getting the following error:
"health_tb.id" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.79
I understand that one reason why I could be getting this error is because id may not exist in health_tb, but that is not the case. I hope someone can advise. Thank you.
First, you should learn to use modern join syntax, although this has nothing to do with your problem:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.id;
A simple search on Google pointed me to the documentation for this error. One of the first things it mentions is:
Possible reasons for this error include:
The specified column is not a column of any of the source or target
tables or views of the statement.
In a SELECT or DELETE statement, the specified column is not a column of any of the tables or views that are identified in a FROM
clause in the statement.
A column list of an SQL data change statement specified the name of a column of the target table or view of the statement.
I suspect that the id column is really called something like user_id. The working query might look like:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.user_id;
1) check if the id column in both tables have the same data type
2) check if there is any trailing space in the column name
select '<' || column_name || '>' from user_tab_columns
where tbname = 'health_tb'
If the id columns are defined as different types, that could be a problem.