Java - Inner Join with 3 Table - java

i doing some project using Java(netbeans sw) and link to Microsoft Access.
The problem occur when i need to inner join 3 tables together from Microsoft Access,
i have no problem to inner join 2 tables together
rsUpdate =
stmtUpdate.executeQuery("SELECT * FROM A_User Inner Join A_PC ON A_USER.SN = A_PC.SN");
which i able to get the result. But not inner join with 3 tables
rsUpdate =
stmtUpdate.executeQuery
("SELECT * FROM A_User Inner Join A_CPU ON A_USER.SN = A_CPU.SN , Inner Join A_Software ON A_CPU.SN = A_Software.SN")
For the SQL above I have 3 "A" table separately for USER | CPU | Software|
USER PK is SN | CPU FK is SN | Software PK is SN |
The Error I got java.sql.SQLException:Characters found after end SQL statement
Thanks

rsUpdate =
stmtUpdate.executeQuery
("SELECT * FROM A_User
Inner Join A_CPU ON A_USER.SN = A_CPU.SN
Inner Join A_Software ON A_CPU.SN = A_Software.SN");
no need for ',' here... try this above code

For Ms Access, when you JOIN more than table, the syntax is different. It should be this way:
SELECT *
FROM ((a_user
INNER JOIN a_cpu
ON a_user.sn = a_cpu.sn)
INNER JOIN a_software
ON a_cpu.sn = a_software.sn)

There should be no comma after the first join
rsUpdate =
stmtUpdate.executeQuery
("SELECT * FROM A_User Inner Join A_CPU ON A_USER.SN = A_CPU.SN Inner Join A_Software ON A_CPU.SN = A_Software.SN")

Problem Solved
For example -
Table A | Username(PK)| Address|
Table B | ID | Phone | Username(FK)|
Table C | SN | Brand | Model | Username(FK)
rs = st.executeQuery
("SELECT * FROM (A Inner Join B on A.Username = B.Username) Inner Join C on A.Username = C.Username");
if anyone looking for inner join 3 tables together by Using JAVA and Link to Access
use the referenece above.
Make sure you must link the table relationship in Access before run the java program if not it will pop out "ERROR IN ROW"
Thanks everyone who helping me :)

Related

Unable to form a single query to join multiple tables

I have to write a single select query to fetch all details from seven tables. Earlier I had separate queries but now I am working on optimizing the queries.
The following is my table structure :
Main Table (SN, model , regId) -- 1 row
Table1(col1,col2....SN) - SN is forienKey -- 1 row
Table2(col1,col2,....SN) - SN is forienKey -- 1 row
Table3(col1,col2,.....regId) - regId is forienKey -- 1 row
Table4(col1,col2,.....regId) - regId is forienKey - 2 rows
Table5(col1,col2,.....regId) - regId is forienKey - 2 rows
Table6(col1,col2,.....regId) - regId is forienKey - 1 row
There is a one to many mapping between the main table and table 4 and table 5.
I have tried using inner join to fetch the rows but I am getting duplicate values. Below is the query that I have written. Is there anything I am missing?
select * from MainTable
inner join Table1 on MainTable.SN = Table1.SN
inner join Table2 on MainTable.SN = Table2.SN
inner join Table3 on MainTable.regId = TABLE3.regId
inner join TABLE4 on MainTable.regId = Table4.regId
inner join TABLE5 on MainTable.regId = Table5.regId
inner join TABLE6 on MainTable.regId = Table6.regId
try joining tables like below to join all of them together
select * from (select * from MainTable
inner join Table1 on MainTable.SN = Table1.SN
inner join Table2 on Table1.SN = Table2.SN ) tempMain
inner join Table3 on tempMain.regId = TABLE3.regId
inner join TABLE4 on TABLE3.regId = Table4.regId
inner join TABLE5 on Table4.regId = Table5.regId
inner join TABLE6 on Table5.regId = Table6.regId
though from your example i don't know if table2 & table 3 are related. anyways you can try to find similar.

Are these SQL queries equivalent? And which is better or is there a better option?

I am working on a Spring web application that utilizes hibernate to connect to a DB2 database. I am try to optimize a service method that gets called may times during a wed service call by reducing the number of DB queries.
So my question is whether or not this query
SELECT DISTINCT a.* FROM TABLE_A a
LEFT JOIN TABLE_B b ON a.ID = b.FK_ID
LEFT JOIN TABLE_C c ON a.ID = c.FK_ID
LEFT JOIN TABLE_D d ON c.DATA_RQST_ID = d.ID
WHERE (b.REQUEST_ID = 1234 AND b.TYPE = 'TYPE_A')
OR (c.REQUEST_ID = 1234 AND (c.TYPE = 'TYPE_A' OR c.TYPE = 'TYPE_B'))
is equivalent/better then this query
SELECT * FROM TABLE_A a
WHERE a.ID IN
(
SELECT b.FK_ID FROM TABLE_B b
WHERE b.REQUEST_ID = 1234 AND eb.TYPE = 'TYPE_A'
)
OR a.ID IN
(
SELECT c.FK_ID FROM TABLE_C
WHERE ( c.REQUEST_ID = 1234 AND c.TYPE = 'TYPE_A' )
OR
(
c.TYPE = 'TYPE_B' AND c.REQUEST_ID IN
(
SELECT d.ID FROM TABLE_D d
WHERE d.REQUEST_ID = 1234 AND v.TYPE = 'TYPE_A'
)
)
)
or is there a better option?
Both queries seem to run about the same time (<50ms) but that may depend on the resulting data. I would need to test more to know for sure.
The point of these two queries is for one of them to replace three other queries where their resulting data is processed in Java to get the required data.
I will also have to be able to convert the SQL query to HQL. I was struggling to convert the first query.
I have a feeling that I maybe wasting my time since the java objects for tables B and C are a one-to-many relationship in the object for table A and they are load by hibernate anyway. Meaning I may not be saving anytime in the long run. Is my thinking here correct?
Thanks!
If I understand correctly, exists would be the best solution:
SELECT a.*
FROM TABLE_A a
WHERE EXISTS (SELECT 1
FROM TABLE_B b
WHERE a.ID = b.FK_ID AND b.REQUEST_ID = 1234 AND b.TYPE = 'TYPE_A'
) OR
EXISTS (SELECT 1
FROM TABLE_C c JOIN
TABLE_D d
ON c.DATA_RQST_ID = d.ID
WHERE a.ID = c.FK_ID AND
c.REQUEST_ID = 1234 AND
(c.TYPE IN ('TYPE_A', 'TYPE_B'))
);
One big gain is just in removing the select distinct.
Then for performance, you want indexes on table_b(fk_id, request_id, type_id) and table_c(fk_id, request_id, type, DATA_RQST_ID) and table_d(id).

Android SQLite Query Syntax causing sqlite.SQLiteException

So i have a section in my database where i need to do a join the tables look like this...
______________ _______________
|Vehicle | |Car |
|--------------| |---------------|
|_id PK | |_id PK |
|Warehouse | |VehicleId FK |
|Location | |Make |
|VehicleKey | |Model |
|______________| |_______________|
The Car VehicleID Foreign key is from the Vehicle Primary key. What I want to return is a cursor for each vehicle.
Warehouse Location Make Model
I'm not sure how I would construct the SQLite statement, can anyone give me a hand?? So far I have this as my method to retrieve all of the vehicles
public Cursor getCars()
{
Cursor c = database.query("SELECT Vehicle.Warehouse, Vehicle.Location, Vehicle.VehicleKey, Car.Make," +
" Car.Make FROM Vehicle INNER JOIN Car" +
"ON Vehicle Vehicle._id = Car.VehicleId",
null, null,null,null,null,null,null);
return c;
}
I'm pretty new to this way of doing database manipulation as I would normally have it done for me by a builder as I have only ever worked in c#. Can anyone help me out trying to get the correct results?
There is an error produced, but I think it's because of syntax of the sqlite statement
android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: SELECT * FROM SELECT Vehicle.Warehouse.........
There are no overloaded methods for query that accept a Sql String as a parameter.
See the documentation for the query methods here.:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
The first parameter is usually the table name.
The system is trying to build a Sql query using your whole query as the table name therefore you get a query such as "SELECT * FROM SELECT Vehicle..." etc
Either supply the correct parameters to the query method or use the rawQuery method which expects a standard Sql string.
String table = "Vehicle v INNER JOIN Car c ON Vehicle v._id = c.VehicleId";
String[] columns = new String[]{ "v.Warehouse", "v.Location", "v.VehicleKey", "c.Make"};
/*String where condition for example "where v.id = 1" 3rd parameter is your where codition in your db.query function */
/*db.query with where condtion : Cursor c = db.query(table,columns,"where v.id = 1",null,null,null,null,null);*/
Cursor c = db.query(table,columns,null,null,null,null,null,null);
Please review your query since i don't see patient Table in your join
You may edit your query using this syntax :
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;

complicated sql query returning empty result

My database looks like this
tickets table
-------------
ticket_id
title
description
department_id
status_id
priority_id
assignee_id
creator_id
departments table
------------------
dep_id
dep_name
status table
------------
status_id
status_name
priority table
---------------
pr_id
pr_name
users table
-----------
u_id
username
password
salt
email
firstName
lastName
department_id
userlevel_id
userlevels table
-----------------
ul_id
ul_name
I need to load all tickets given the asignee id. My query looks like this
SQLQuery q = q.createSQLQuery("SELECT t.*,d.*,s.*,p.*,u.*,a.* FROM tickets t, departments d, status s, priority p, users u, attachments a WHERE t.department_id=d.dep_id AND t.status_id=s.stat_id AND t.priority_id=p.pr_id AND t.assignee_id=u.u_id AND t.creator_id=u.u_id AND t.tick_id=a.ticket_id AND assignee_id=?");
q.setInt(0, some_valid_assignee_id);
List<Object> result = q.list();
But it's returning an empty object list. Can anyone point me in the right direction, thanks!!
In your query you have AND t.assignee_id = u.u_idand also AND t.creator_id = u.u_id. The only way that would return any records is if the assignee_id and creator_id were the same. I think what you really need to do is link to the users table twice like so:
SELECT t.*, d.*, s.*, p.*, u1.*, u2.*, a.*
FROM tickets t
INNER JOIN departments d ON t.department_id = d.dep_id
INNER JOIN status s ON t.status_id = s.stat_id
INNER JOIN priority p ON t.priority_id = p.pr_id
INNER JOIN users u1 ON t.assignee_id = u.u_id
INNER JOIN users u2 ON t.creator_id = u.u_id
INNER JOIN attachments a ON t.tick_id = a.ticket_id
WHERE assignee_id = ?
You should use proper join syntax . . . makes the query easier to read and to write.
However, I'm guessing that the problem is:
t.assignee_id = u.u_id AND t.creator_id = u.u_id
That would assume that
t.assignee_id = t.creator_id
Perhaps this never happens in your data.

JAVA mySQL JOIN 2 tables

Hey guys I'm looking to get information out of 2 tables to create a JTABLE with that information.
The tables I am look at are 'shipments' and 'customers'
Where shipments takes the form of
shipNumber | shipperID | destID | size | weight
and customers takes the form of
ID | lastName | firstName | street | city | state | zip
The shipperID and destID both refer to a customer ID.
I am trying to get the city/state information out of the customers table that corresponds to the shipperID and destID.
I have tried the following
query = "SELECT shipments.shipNumber, customers.city, customers.state, customers.city, customers.state FROM shipments, customers WHERE shipments.shipperID = customers.ID";
Realizing that the duplicate customers.city/customers.state is populating the same information twice.
As previously said, I am trying to get the shipper city/state and destination city/state.
I also tried
query = "SELECT shipments.shipNumber, customers.city, customers.state, customers.city, customers.state, shipments.size"
+ " FROM shipments"
+ " INNER JOIN customers ON customers.id = shipments.shipperID";
Where this gives the same information.
I am not sure how to reference the destID = customer.id
Thanks,
Mike
The usual trick is to join with the customers table twice, once for the shipper and once for the destination:
SELECT shipments.shipNumber,
shipper.city, shipper.state,
dest.city, dest.state,
shipments.size
FROM shipments
INNER JOIN customers shipper ON shipper.id = shipments.shipperID
INNER JOIN customers dest ON dest.id = shipments.destID

Categories