How would I self join a table to show the Name, employee number, manager's name of those who are managed by either Blake or Jones?
I'm trying to line it up in the following manner:
SELECT
FROM
INNER JOIN
ON
WHERE
The problem I am having is I have understood MySQL very well up until now, and I cannot seem to grasp the concept of the table joining itself.... any help would be appreciated. Thanks in advance
MySQL self join that joins a table to itself using join
SELECT *
FROM table1 AS t1
INNER JOIN table1 AS t2
ON t1.col_name=t2.col_name
WHERE t1.col_name='xyz'
select t1.name,t1.employee_number,t1.manager_name from table t1 join
table t2 where t1.manager_name = t2. manager_name and t2.manager_name in
('Blake','Jones');
Related
How would I self join a table to show the Name, employee number, manager's name of those who are managed by either Blake or Jones?
I'm trying to line it up in the following manner:
SELECT
FROM
INNER JOIN
ON
WHERE
The problem I am having is I have understood MySQL very well up until now, and I cannot seem to grasp the concept of the table joining itself.... any help would be appreciated. Thanks in advance
MySQL self join that joins a table to itself using join
SELECT *
FROM table1 AS t1
INNER JOIN table1 AS t2
ON t1.col_name=t2.col_name
WHERE t1.col_name='xyz'
select t1.name,t1.employee_number,t1.manager_name from table t1 join
table t2 where t1.manager_name = t2. manager_name and t2.manager_name in
('Blake','Jones');
I'm trying to add the "char_hours_flown" from the first table, grouped by "emp_num" from the second table so I can see how many hours each crew member has accrued.
2 Tables
Try using join
select emp_num, sum(char_hours_flown)
from tablename2 a inner join tablename1 b on a.char_trip=b.char_trip
group by emp_num
This should be very straight forward based on what you have asked?
select sum(char_hours_flown)
,emp_num
from table
group by emp_num
You can try like following.
select sum(T1.CHAR_HOURS_FLOWN) sumoflownHours, T2.EMP_NUM
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON T1.CHAR_TRIP=T2.CHAR_TRIP
GROUP BY T2.EMP_NUM
Note: Replace the name of TABLE1 and TABLE2 appropriately.
I want to display the ID from Table1 (TID) and the results of an inner join.
The following statement is not working.
Situation: Two Tables:
Table 1 PK:TID, FK: Table2_PID
Table2 PK: PID, Name
Among other data I want to display the Name of every PID in Table1 which is stored in Table2.
SELECT T.TID
,(Select P.Name
from mydb.Table2 P
inner join mydb.Table1 T
on P.PID=T.Table2_PID)
FROM mydb.Ticket T;
Result: Error Code 1242. Subquery returns more than 1 row
I do know that the result returns more than 1 row, but I want to show the Name of every PID in Table1 which is stored in Table2. So any ideas on how I can do that?
PS: I'm using mySQL and working with MySQL Workbench v6.3
You don't need use the inner joins for getting all the names of the ID. You can try the default join to achieve the result.
select t2.pid, t2.name from mydb.Table2 t2, mydb.Table1 t1 where t1.pid = t2.pid;
Hope this helps.
You must use join like this
select t1.TID,t2.Name from Table1 t1 left join Table2 t2 on t1.Table2_PID=t2.PID
Thanks for the response, but the question/problem still remains.
It wasn't about the join.
It is about the subquery and selecting multiple rows within it.
Thank you guys,
I was thinking of a solution way to complicated. I resolved it using a simple where statement.
SELECT T.TID, P.Name
FROM mydb.table1 T, mydb.table2 P
WHERE P.PID=T.table2_PID;
I am a newbie at MySQL..... I am trying to left join 3 tables one contains some_id,name,count,descr and second one has id,some_id,uni_id and the last one has uni_id,price,added,etc So when i try to join these three tables it says that there's no such field named descr
What could be the best way to join these tables without modifying structure of them?
Assuming the following schema:
table1(some_id, name, count, descr), where some_id is the primary key;
table2(id, some_id, uni_id), where some_id is a foreign key to table1;
table3(uni_id, price, added), where uni_id is a foreign key to table2.
All you need to do is a LEFT OUTER JOIN between the three tables:
SELECT *
FROM table1 t1 LEFT JOIN table2 t2 ON (t1.some_id = t2.some_id)
LEFT JOIN table3 ON (t2.uni_id = t3.uni_id)
References:
Left Outer Join
Join Syntax
It would be ideal if you could post the schema for your tables. Without seeing the query, it sounds like you've made a reference to a field that you may have aliased to the wrong table.
At the most basic level, "descr" doesn't exist as you've tried to reference it, but beyond that, its hard to say without seeing the query itself.
SELECT descr
FROM table1
LEFT JOIN table2 ON table2.some_id = table1.some_id
LEFT JOIN table3 ON table3.uni_id = table2.uni_id
Should do the trick.
Can anyone help me please? Inner join query working fine. but query displaying duplicate data. I don't to display duplicate data.
here is my query.
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
Here is output
"COLOR";"456";"456";"Nude"
"COLOR";"456";"456";"Ivory"
"COLOR";"456";"456";"Black"
"COLOR";"456";"456";"Coral"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Coral"
"COLOR";"459";"459";"Nude"
"COLOR";"459";"459";"Ivory"
"SIZE";"460";"460";"Large"
"SIZE";"460";"460";"Medium"
"SIZE";"460";"460";"Small"
"SIZE";"470";"470";"Large"
"SIZE";"470";"470";"Small"
"SIZE";"470";"470";"Medium"
"COLOR";"476";"476";"White"
"COLOR";"476";"476";"Black"
"SIZE";"477";"477";"Small"
But i don't to display duplicate data. for example which is displaying here.
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"460";"60";"Black"
is there any way?? thanks
Maybe you just want to group it by names? You seem to be calling a duplicated data what seems to have different ids...
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
GROUP BY t1.class,t2.option_name