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';
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")
I have a query with columns (code, store, slotAvailable) in which results will be shown. As for the column "slotAvailable", it will contain the number of free slots for that particular "store". Now, to calculate how many free slots there are, I can only do this if I do a join with the table "TimeSlotInstanceReservation" because in this table there are a field "slotConsumed" that I need to perform the subtraction with the capacitytotal field of table "TimeSlotInstance" :
TimeSlotInstance as tsi
join TimeSlotInstanceReservation as tsir on {tsir. timeSlotInstance} = {tsi.pk}
However, when I do not have a reservation on a given TimeSlotInstance, no result is shown in the column "slotAvailable". How can I write a condition in the query that says "if there is no reservation then give me this value otherwise join with the reservation"? I hope I have explained myself well.
this query failed but not show error:
SELECT
{a.code} as 'Code',
{a.name} as 'Name',
({{
SELECT
{tsi.capacity} - IFNULL({tsir.slotsConsumedCount},0) as slot1820
FROM
{
TimeSlotInstance as tsi
join TimeSlotHour as tsh on {tsi.timeSlotHour} = {tsh.pk}
join StoreManager as sm on {sm.pk} = {tsi.store}
left Join TimeSlotInstanceReservation as tsir on {tsi.pk} = {tsir.timeslotinstance}
}
where {tsh.starttimelabel} = '18:00' and {tsh.endtimelabel} = '20:00' and {tsi.day} = '2022-07-26T22:00'
}}) as 'SLOT Available 18/20'
FROM
{
TimeSlotInstance as tsi
join TimeSlotHour as tsh on {tsi.timeSlotHour} = {tsh.pk}
}
where {tsi.day} = '2022-07-26T22:00'
but this work:
select
{tsi.capacity} - IFNULL({tsir.slotsConsumedCount}, 0) as SLOT1820
from
{
TimeSlotInstance as tsi
join TimeSlotHour as tsh on {tsi.timeSlotHour} = {tsh.pk}
left join TimeSlotInstanceReservation as tsir on {tsi.pk} = {tsir.timeslotinstance}
}
where {tsh.starttimelabel} = '18:00' and {tsh.endtimelabel} = '20:00' AND {tsi.day} = '2022-07-26T22:00'
You want a LEFT JOIN!
https://www.w3schools.com/sql/sql_join_left.asp
A LEFT JOIN between two tables returns all the records from the table on the left, plus any record from the table on the right that match the join condition. In the case where no record from the right table matches the join condition, nulls are returned for those columns, though the columns from the left table will be populated.
Hence your query would look something like
SELECT tsi.code, tsi.store,
tsi.capacitytotal - IFNULL(tsir.slotConsumed, 0) as slotAvailable
FROM TimeSlotInstance tsi
LEFT JOIN TimeSlotInstanceReservation tsir on tsi.pk = tsir. timeSlotInstance
Note that if the TimeSlotInstance -> TimeSlotInstanceReservation relationship is one-to-many you will need additional criteria in a WHERE clause to exclude dupe instances. If so, be aware that your conditions must allow for NULL values in the TimeSlotInstanceReservation table or you will exclude the unmatched records from TimeSlotInstance.
I'm trying to compare if a single column of my table equals to what the subquery returns in multiple columns. I want to be able to return rows if table1.id is equal to either one of table2.first or table2.second. What would be the valid syntax to do it?
I've tried what's below ,but it's not valid syntax as I get "Result: sub-select returns 2 columns - expected 1."
SELECT table1.id, table1.attr1, table1.attr2 FROM table1 WHERE table1.id IN (SELECT table2.first, table2.second FROM table2 WHERE type != "..." AND type != "someType")
Perhaps you intend something like this:
SELECT
t1.id,
t1.attr1,
t1.attr2
FROM table1 t1
WHERE
EXISTS (SELECT 1 FROM table2 t2 WHERE t2.first = t1.id AND type <> 'someType') OR
EXISTS (SELECT 1 FROM table2 t2 WHERE t2.second = t1.id AND type <> 'someType');
Each EXISTS clause checks for a possible match of a table1.id value against a table2 column.
You don't need subquery if you want to select from t1 only if the conditions match. Try below query:
SELECT
t1.id,
t1.attr1,
t1.attr2
FROM table1 t1 , table2 t2
WHERE
(t1.id = t2.first OR t1.id = t2.second)
AND t2.type not in ('someType' , "...");
You can do it with an INNER JOIN:
SELECT DISTINCT t1.id, t1.attr1, t1.attr2
FROM table1 t1 INNER JOIN table2 t2
ON t2.first = t1.id OR t2.second = t1.id
WHERE t2.type <> "..." AND t2.type <> "someType"
The ON clause can be written as well like:
ON t1.id IN (t2.first, t2.second)
I used DISTINCT in case table1.id could match multiple rows in table2.
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
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