MySQL error 1064 connecting 3 tables query - java

SELECT *
FROM employee e FULL OUTER JOIN salarydetails s ON (e.Eid=s.Eid)
WHERE s.Month="January" AND e.Eid="0002"
UNION
SELECT *
FROM salarydetails s FULL OUTER JOIN bonus b ON (s.Eid=b.Eid)
WHERE b.Month="January" AND b.Eid="0002"
I have 3 tables: employee, salarydetails, and bonus. I’m writing a Java code to load a JTable. I have to get the data from these three tables. The query I’m trying to use is the above one. But it gives errors. What am I supposed to do?

Related

Do not join 2 tables but paging with them

I have two oracle table:users and logs,as follow,
users:
---------
id number,
name varchar2(50)
logs:
-----
user_id number,
visit_date date
I need to find out the first 10 users who never appears in the logs table (means they never visit the page),I can write a sql:
select *
from users u
where not exists (
select 1 from logs o
where o.user_id = u.id)
and rownum<=10
But now the problem is these two table are not in the same schema,they could not be accessed for each other,so I can not join them.
Because there are two microservices,one microservice should query its own schema
If I fetch 10 records from users,maybe they all never visit the page,I need to fetch 10 more,then after that still 5 users did not visit that page,I need to fetch 5 more,and so on.So I am not sure how many record I should fetch at the first time.Is it a good idea to do so?And how to write the code?
If I fetch all the record of the 2 tables,then do join using Java code,I'm worried about the amount of data being too large.Or should I just make these two tables accessible to each other?
So,how to do paging with 2 table but not join them?
But now the problem is these two table are not in the same schema,they could not be accessed for each other,so I can not join them.
Create a user that can see both schemas and log in to that user and access the tables using schema_name.table_name:
SELECT *
FROM schema1.users u
WHERE NOT EXISTS (
SELECT 1
FROM schema2.logs l
WHERE l.user_id = u.id
)
ORDER BY u.something
FETCH FIRST 10 ROWS ONLY
or
SELECT *
FROM (
SELECT *
FROM schema1.users u
WHERE NOT EXISTS (
SELECT 1
FROM schema2.logs l
WHERE l.user_id = u.id
)
ORDER BY u.something
)
WHERE ROWNUM <= 10
Alternatively, create a third schema and GRANT the SELECT privilege on the USERS and LOGS tables in the first two schemas and then create a view in that third schema and a new micro-service and use that.

#Query for joining two tables

I am trying to join two entities in a third one using #Query method.
#Query("SELECT new com.concretepage.entity.DeptEmpDto(d.departmentId,d.departmentName,d.managerId,d.locationId,e.employeeId,e.firstName,e.lastName,e.phoneNumber,e.hireDate,e.jobId,e.salary,e.commissionPct) FROM Employee e INNER JOIN Department d")
List <DeptEmpDto> fetchEmpDeptDataInnerJoin();
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1.
I cannot understand where is my mistake.Any help will be appreciated :).
You missed the joining condition after joining tables using ON clause. So just change your query with this:
#Query("SELECT new com.concretepage.entity.DeptEmpDto(d.departmentId,d.departmentName,d.managerId,d.locationId,e.employeeId,e.firstName,e.lastName,e.phoneNumber,e.hireDate,e.jobId,e.salary,e.commissionPct) FROM Employee e INNER JOIN Department d on e.joining_column_from_table1=d.joining_column_from_table2")
Make sure to replace joining_column_from_table1 and
joining_column_from_table2 with your column names from table
Employee and Department respectively

How to write this query with hibernate using mysql DB

I am trying to select one row from all 3 tables. The tables have the same structure, 4 columns, "code", "airportname", "latitude", "longitude".
The query I am trying to write is:
select
finnairroute.code,finnairroute.airportname,finnairroute.latitude,finnairroute.longitude,lhroute.airportname,lhroute.latitude,lhroute.longitude,sasroute.code,sasroute.airportname,sasroute.latitude,sasroute.longitude
FROM frs.sasroute, frs.finnairroute,frs.lhroute where
sasroute.code="BER" or finnairroute.code ="BER" or lhroute.code
="BER";
Tested with Mysql workbench and I got this result:
How should I write the query in order to get one single row that has all the info for "BER"?
Many thanks!
You can try something like this:
SELECT finnairroute.*,lhroute.*,sasroute.* FROM DUAL
LEFT OUTER JOIN finnairroute ON (finnairroute.code='BER')
LEFT OUTER JOIN lhroute ON (lhroute.code='BER')
LEFT OUTER JOIN sasroute ON (sasroute.code='BER')
The thing is that you will always get one row, even if the code is in none of the table. You need to test whether finnairroute.code, lhroute.code and sasroute.code are null or not.
Another way to approach the problem:
SELECT 'finnair' as company,code,airportname,latitude,longitude FROM finnairroute WHERE code='BER'
UNION SELECT 'lh' as company,code,airportname,latitude,longitude FROM lhroute WHERE code='BER'
UNION SELECT 'sas' as company,code,airportname,latitude,longitude FROM sasroute WHERE code='BER'
This gives you between zero and three rows, depending on what tables contain the code 'BER'. An additional column, 'company' tels you which table contains each row.

SQL generated by Hibernate invalid for MS SQL

Hibernate (HQL) generated the following SQL for which I inserted the parameters:
select
sum(activity0_.calculated_work) as col_0_0_
, employee2_.id as col_1_0_
, projectele1_.id as col_2_0_
from
activity activity0_
inner join generic_object activity0_1_ on activity0_.id=activity0_1_.id
left outer join project_element projectele1_ on activity0_.project_element_id=projectele1_.id
left outer join employee employee2_ on activity0_.owner_id=employee2_.id
left outer join org_unit orgunit3_ on employee2_.org_unit_id=orgunit3_.id
where
activity0_1_.deleted=0 and
activity0_.client_id=22
group by
employee2_.id order by SUM(activity0_.calculated_work) DESC
Error message: Column 'project_element.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I executed this SQL directly in the SQL Server Studio with the same result. I commented this line:
, projectele1_.id as col_2_0_
The SQL was then accepted by the SQL Server
The table project_element definitely has a column with the name id it is also referenced in the LEFT OUTER JOIN and there this column is not causing an error.
Removing the alias projectele1_ had no effect.
To me this looks like a really simple SQL statement. I cannot imagine what is wrong with it and the error message is not helping at all. Any ideas what could be wrong with the SQL?
Your SQL syntax is wrong.If you add projectele1_.id to group by clause it will work.Only aggregate functions work in select statement with group by clause.Or if you remove projectele1_.id from select it will work fine.
My mistake. I should have read the error message several times. projectele1_id is not in the group by clause. MS SQL does not allow to include such a column into the select list. This seems to be a consistency check.
Too bad though that the usage of HQL leads to such an exception in SQL Server but not in MySQL Server.

SQL full join in Java

I'm trying to join two tables but can't get the SQL right. My SQL statement is far more complex than the below SQL but the below SQL will do to show the principle. I get an invalid identifier exception when trying to refer to t or p. How come I cannot refer to these? I'm using Oracle DB.
SQL:
SELECT * FROM ((SELECT * FROM transactions t) FULL JOIN (SELECT * FROM payments p) ON (t.id = p.trans_id));
Exception:
Caused by: java.sql.SQLException: ORA-00904: "P"."TRANS_ID": invalid identifier
You are not really giving your querys an alias, you should do:
SELECT *
FROM (SELECT * FROM transactions) t
FULL JOIN (SELECT * FROM payments) p
ON (t.id = p.trans_id);
Of course, assuming that you are using real derived tables instead of those simple SELECTs, otherwise you should just use the tables directly.
The aliases are in the wrong place. The alias for a derived table should be just after the closing parenthesis:
SELECT *
FROM
(SELECT * FROM transactions) t
FULL JOIN
(SELECT * FROM payments) p
ON t.id = p.trans_id
Note that it makes no sense to use derived tables with such a simple query. I assume that you do actually need the derived tables and that you just simplified your query for the question.

Categories