How to write this query with hibernate using mysql DB - java

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.

Related

How to include redundant column to show in SELECT Query from second table instead from the main table in Spring Repository?

I have a query like this:
Query 1:
select *
from items item
This is bind to an entity in SpringJPA.
Now I have to select one more column from another table like below.
Query 2:
select item.*,is.id
from items item
inner join item_state itm_s where item.id=itm_s.id
Now,there is a column "code" which exists in both item and item_state. I want to select it from item_state and not from item due to some data issues. I know the straight forward way is to write all columns in select statement,excluding code column from item and including code column from item_state.
But the thing is item table has around 100 columns.
Is there a good way to solve this issue without changing the entity class?
Look at Spring Data JPA projections and perform the following query:
#Query("select item.*, itm_s.code from items item join item.item_state itm_s where item.id = itm_s.id")
You will have to look at the resulting query to write your projection class.

Adding a column from another table into existing SQL/query statement

Currently, I have a query that I run in my java code that displays just a simple grid output of columns with the corresponding data for those specific fields. I am reading 2 tables that all have the same column names. I need to add just 1 column to that grid, but the field name resides on a different table. How would I add this to my existing query?
This is my current query that I execute in the Java:
SELECT TRNSP_EQP_EIN, TRNSP_EQP_ID, PRE_EQP_ID, EQP_GRP, AAR_CT_C,
AAR_MCHDSG_C,BLD_D, REBLD_D
FROM EQ.TE_TRNSP_EQPACTV A
WHERE TRNSP_EQP_ID = ‘BNSF0000000123’
UNION
SELECT TRNSP_EQP_EIN, TRNSP_EQP_ID, PRE_EQP_ID, EQP_GRP, AAR_CT_C,
AAR_MCHDSG_C,BLD_D, REBLD_D
FROM EQ.TE_TRNSP_EQPHIST A
WHERE A.TRNSP_EQP_ID = ‘ABC0123’
ORDER BY TRNSP_EQP_EFF_TS
WITH UR
Below is the information that I am trying to add to the grid to the existing SQL.
Table: EQ.TE_LOCO_EQP
Field: DEL_RSN_CD
You need to provide us with the full field list of EQ.TE_LOCO_EQP to answer this question in full, yet I think you'll be able to manage with what I have provided you below. Replace what is in the square brackets ([]) with the field relevant for the join.
I agree with #Snowman in that you could easily research this one.
SELECT
*
FROM
(
SELECT
TRNSP_EQP_EIN
,TRNSP_EQP_ID
,PRE_EQP_ID
,EQP_GRP
,AAR_CT_C
,AAR_MCHDSG_C
,BLD_D
,REBLD_D
FROM
EQ.TE_TRNSP_EQPACTV A
WHERE
TRNSP_EQP_ID = ‘BNSF0000000123’
UNION
SELECT
TRNSP_EQP_EIN
,TRNSP_EQP_ID
,PRE_EQP_ID
,EQP_GRP
,AAR_CT_C
,AAR_MCHDSG_C
,BLD_D
,REBLD_D
FROM
EQ.TE_TRNSP_EQPHIST B
WHERE
A.TRNSP_EQP_ID = ‘ABC0123’
ORDER BY
TRNSP_EQP_EFF_TS
WITH UR /* No idea what this is? */
) X
LEFT JOIN
EQ.TE_LOCO_EQP Y
ON
X.[PRIMARY_KEY] = Y.[EQUIVELANT_FOREIGN_KEY]

MySQL error 1064 connecting 3 tables query

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?

Update query wih join in HQL

I have two tables RootOprs and ChildOprs, where RootOprs can have multiple ChildOprs
I want to update 2 column in RootOprs table and one column in ChildOprs with bulk data
Update ChildOpr c
Left join Fetch c.rootOpr
set c.status=:status,
r.status=:staus
r.oprtimestamp=:timestamp
where c.id IN :childOperIDList;
This query is not working. Does anyone have better approach than this? Please help.

Combine two resultset from different table

My Requirement is to display some of the columns in one table and some of the columns in another table in an html table. Though it has same coloumn id , value will be different. So, I cannot match this two. My query is as follows:
SELECT time_stamp,queryresultset FROM table1 d WHERE dID = 'CP009'
AND d.time_stamp >'2011-05-01 00:00:00' AND d.time_stamp < '2011-05-01 05:00:00'
order by time_stamp
UNION ALL
SELECT time_stamp,cpuutil FROM table2 h WHERE hID='HS002'
AND h.time_stamp >'2011-05-01 00:00:00' AND h.time_stamp < '2011-05-01 05:00:00'
order by time_stamp
So, the time_stampe here I'm getting will vary just in milliseconds for both the table. But, I want it in one resultset. Though the time value varies in millisecones between the table, the number of rows will be equal. So, I have to bring this in a single resultset. I don't know whether it is possible to handle in sql query. Or I may have to try in java coding? Please guide me. Following is my sample html table.
----------------------------------------------------
Time_stamp Cpuutil Queryresultset
----------------------------------------------------
2011-03-09 12:00:00 2.3 9.8
2011-03-09 12:15:00 5.3 4.5
2011-03-09 12:30:00 4.3 9.3
2011-03-09 12:45:00 2.3 9.2
I am afraid, I have difficulties with understanding your question, but it seems to me, you are looking for something as:
SELECT table1.time_stamp t1, table1.queryresultset, table2.time_stamp t2, table2.cpuutil
FROM table1 , table2
WHERE ABS(t1-t2)<100
AND t1 >'2011-05-01 00:00:00'
AND t1 < '2011-05-01 05:00:00'
ORDER by t1
Another posibility:
`SELECT column list
FROM table1
INNER JOIN table2
ON table1.col1=table2.col2
WHERE criteria
ORDER BY column list `
Have a nice day.
This sounds like a database conception mistake. If your two tables are linked logically, they should have a physical connector. If you can refactor your database model, the best solution is to add a table table0 holding the common factors between the two tables (or just an autoincrement id), and then to add an external key to table1 and table2. You need to insert a row into table0 first, then to insert a row into table1 and one into table2 using table0's key as an external key.
If you can't refactor the tables, that's too bad. Anyway, the easiest way to do what you want is to write java code:
Open resultset1 from table 1
Open resultset2 from table 2
While resultset1 is not empty
fetch row1 from resultset1
fetch row2 from resultset2
generate html
Close resultsets
Done!
But this way is wrong and you will get problems as you advance further in your project.
Thanks for your responses. So I found out the query. It's working now.
SELECT a.cpuutil,a.hostid,a.time_stamp, b.queryresultset, b.time_stamp AS tm
FROM table1 a, table2 b
WHERE a.hID = 'hs002' AND b.dID='cp011'
AND SUBSTR(a.time_Stamp,1,15) = SUBSTR(b.time_stamp,1,15)
AND a.time_stamp > '2011-05-10 00:00:00'
AND a.time_stamp < '2011-05-10 14:00:00'

Categories