I have 3 tables that I want to join in a single table collaborator with this Sql query :
select * from (
select user.id, Project.id, Task.id
inner join Project on user.join_key = Project.join_key
inner join Task on task.join_key = = Project.join_key
) collaborator
And I can't seem to find How to.
I can use this also :
CREATE TABLE Collaborator AS
SELECT user.id, Project.id, Task.id
FROM Project p, Task t, User u
inner join Project on user.join_key = Project.join_key
inner join Task on task.join_key = = Project.join_key.
Any help would be appreciated. Thanks
First create the table
Collaborator
CREATE TABLE `Collaborator` (
`USER_id` int(11),
`PROJECT_id` int(11),
`TASK_id` int(11)
) ENGINE=InnoDB
And then excecute the insert
INSERT INTO Collaborator
SELECT user.id, Project.id, Task.id
FROM Project p, Task t, User u
inner join Project on user.join_key = Project.join_key
inner join Task on task.join_key = = Project.join_key
Related
I want to write a complex hql query with relationships from 3 tables. It is necessary to make a selection of the selected columns so that they are located by ID
#Query("SELECT p.regId, p.method, p.tax, p.fee, p.netAmount, r.countSec, p.status " +
"FROM P p INNER JOIN R r INNER JOIN D d on p.regId = r.id AND p.regId = d.id")
List<P> findAllByRegId(String regId);
My compiler cannot execute the request, I don't understand what the problem is! Help, please
org.postgresql.util.PSQLException: ERROR: syntax error at or near "join"
The format for multiple joins is
SELECT columns ...
FROM table1 AS t1
INNER JOIN
table2 AS t2
ON t1.id = t2.id
INNER JOIN
table3 AS t3
ON t3.id = t1.id
Your join is totally wrong. You have to add ON keyword and map primary key and foreign key in first inner join.
Here is your join, which is wrong
SELECT columns FROM A a
INNER JOIN B b
INNER JOIN C c ON a.ColName = b.ColName AND a.ColName = c.ColName
Here is the correct syntex for multiple join
SELECT columns FROM A a
INNER JOIN B b ON a.ColName = b.ColName
INNER JOIN C c ON a.ColName = c.ColName
Here down is modified query
#Query("SELECT p.regId, p.method, p.tax, p.fee, p.netAmount, r.countSec, p.status " +
"FROM P p INNER JOIN R r ON p.regId = r.id INNER JOIN D d ON p.regId = d.id")
Select concat(substr(T_data,1,9),'001 ') AS Test_Data from DB1.T1 ;
Select * from DB1.T2 WHERE Test_Data = 'Test_Data';
I need to join the DB1.T1 and DB1.T2 based on Test_Data
Use an INNER JOIN (or, if you want nulls, an outer join)
Select t2.*
from DB1.T1 t1
INNER JOIN DB1.T2 t2 ON concat(substr(t1.T_data,1,9),'001 ') = t2.Test_Data
WHERE t2.Test_Data = 'Test_Data';
I have two tables in my database.One is called
Table_join and it has fields
ID , NameOfObject,Address,Date
The second table is called Connection,and it has fields:
ID,IDofGroup,IDofSubgroup.
When I am inserting new record,It can be an object without a subgroup,something like Object1 on on its own,or it can be an Object2, and Object21 that is a subgroup of Object2 - in that case in the second table Connection I would insert id of the Object2 into IDofGroup,and ID of the Object21 into IDofSubgroup.
I want to search my table,so for example when I type in Object2,in the table I wanna see Object2 and its address and date,and Object21 and its date address.When I use inner join,I only get Object21 when I search Object2,not Object2 itself.
also,when I use inner join,i only get id from object21,I need to get the name also.
My select query is
String query = "SELECT * from table_join INNER JOIN connection ON table_join.id = connection.id_IDofSubgroup WHERE NameOfObject=?";
Both Object2 and Object21 are stored in the Table_join table. That means you need to look at two different record from that table at the same time. That means you need that table in the FROM/JOIN clause twice.
Since you're returning two rows from the same table at the same time, your column names will clash, so you have to rename them in the query.
SELECT t1.ID AS MainID
, t1.NameOfObject AS MainName
, t1.Address AS MainAddress
, t1.Date AS MainDate
, t2.ID AS SubID
, t2.NameOfObject AS SubName
, t2.Address AS SubAddress
, t2.Date AS SubDate
FROM Table_join t1
JOIN Connection c ON c.IDofGroup = t1.ID
JOIN Table_join t2 ON t2.ID = c.IDofSubgroup
WHERE t1.NameOfObject = ?
I have such domain: Country has cities, city has offices, offices has services. I need to find all countries with offices which provide specified service id. My current version is:
SELECT c FROM Country c
WHERE EXISTS(
SELECT ct FROM c.cities ct
WHERE EXISTS(
SELECT o FROM ct.offices o
WHERE EXISTS(
SELECT s.id FROM o.services s
WHERE s.id = :id
)
)
)
So, I'm new to HQL. What is best way to this? Is my version Ok? I was thinking about SELECT DISTINCT with LEFT JOIN too.
I don't think there is anything wrong with your query, but this might be more readable.
SELECT c FROM Country
WHERE EXISTS (
SELECT s.id FROM
c.cities ct
JOIN ct.offices o
JOIN o.services s
WHERE s.id = :id
)
I have a table and a class marked as #Entity with
#Table(name="Employee")
It has two fields, Id and Name
i want to do left join on same table like,
Select t2.name, count(*)
from Employee t1 left join Employee t2 on t1.Id = t2.Id
where t1.Name = 'asd'
group by t2.Name
I had a workaround, I converted that query into sub query
Select EmployeeID From Employee
Where ManagerID in (Select EmployeeID where performance >= 4 )
And used DetachedCriteria and i got it worked. But this will reduce my performance. SO i will try your method and see if i can get it worked
Ref
https://forum.hibernate.org/viewtopic.php?p=2389790