staff table:
id name
1 tony
2 sony
3 bony
4 rony
etc.
xyz table:
current staff, next staff, hod staff, vp staff.
1 2 3 4
2 3 1 4
4 3 2 1
etc.
when is run
`select * from xyz,`
I need a query which instead of giving me id it will actually give me names of that staff from staff table.
You need to join staff table more than once
SELECT b.name AS current_staff,
d.name AS next_staff,
c.name AS hod_staff,
e.name AS vp_staff
FROM xyz a
INNER JOIN staff b
ON a.current_staff = b.id
INNER JOIN staff c
ON a.hod_staff = c.id
INNER JOIN staff d
ON a.next_staff = d.id
INNER JOIN staff e
ON a.vp_staff = e.id
If I understand you right, I guess you just could use a multiple join here:
select a.name, b.name, c.name, d.name from xyz
inner join staff as a on a.id = xyz.currentstaff
inner join staff as b on b.id = xyz.nextstaff
inner join staff as c on c.id = xyz.hodstaff
inner join staff as d on d.id = xyz.vpstaff
Related
I want to join output of one query with the second table in mysql:
(select
A.name, A.address, sum(C.penalty_points) as points
from
restaurant as A
inner join
inspection as B ON A.restaurant_id = B.restaurant_id
inner join
violation as C ON C.violation_id = B.violation_id
group by A.name )
Output:
name address points
Kitchen 305 660 Washington Ave 2
PL8 Kitchen Southeast 17th Street in Fort Lauderdale 11
Prime One Twelve 112 Ocean Drive 5
Seasons 52 Palm Beach Gardens 3
Six Tables 32 East Atlantic 8
Table 26 Park Racks Downtown Eatery 2
The result of the second table:
select * from health_points
Output:
points health_grade
0 A
1 A
2 A
3 A
4 A
5 B
6 B
7 B
8 B
9 B
10 B
11 C
12 C
13 C
14 C
15 C
17 FAIL
18 FAIL
19 FAIL
Is there a way I can combine the first query with the second table and extract the health grade ? I was trying something like:
(select
A.name, A.address, sum(C.penalty_points) as points
from
restaurant as A
inner join
inspection as B ON A.restaurant_id = B.restaurant_id
inner join
violation as C ON C.violation_id = B.violation_id
group by A.name ) as D inner join health_points as E on D.points = E.points
But it is showing error in mysql? Any pointers where I am going wrong here?
You are missing the outer SELECT clause:
SELECT D.*, E.health_grade
FROM (
SELECT A.name, A.address, sum(C.penalty_points) as points
FROM restaurant A
JOIN inspection B ON (A.restaurant_id = B.restaurant_id)
JOIN violation C ON (C.violation_id = B.violation_id)
GROUP BY A.name
) D
JOIN health_points E ON (D.points = E.points)
You can do this:
SELECT
e.health_grade,
d.points
FROM
(
select
A.name, A.address, sum(C.penalty_points) as points
from restaurant as A
inner join inspection as B ON A.restaurant_id = B.restaurant_id
inner join violation as C ON C.violation_id = B.violation_id
group by A.name, A.address
) as D
inner join health_points as E on D.points = E.points
I have database with following data structure with sample data. Each company have multiple members. The relationship is in the company_member table. Please note only required fields I have given below.
company
id title
1 company-1
2 company-2
company_member
companyid memberid
1 1
1 2
1 3
2 4
2 5
2 6
member
id firstname member_type_id
1 Name-1 2
2 Name-2 3
3 Name-3 3
4 Name-4 3
5 Name-5 2
6 Name-6 1
member_type
id user_level
1 0
2 1
3 2
I want list of unique companies with one member from each. But the member should be the lowest user_level within the company. i.e, following result should come;
result
companyid company_title memberid member_name user_level
1 company-1 1 Name-1 1
2 company-2 6 Name-6 0
I want to know how to get one member with lowest user level among the same company.
This is a bit complicated one, however this is one way of doing it using not exists, for bigger tables its wise to use not exits since using pivot tables it will not use index.
select
c.id,
c.title,
m.id as member_id,
m.firstname,
mt.user_level
from company_member_map cmp
join company c on c.id = cmp.companyid
join member m on m.id = cmp.memberid
join member_type mt on mt.id = m.member_type_id
where not exists
(
select 1 from company_member_map t1
join member t2 on t2.id = t1.memberid
join member_type t3 on t3.id = t2.member_type_id
where
t1.companyid = cmp.companyid
and t3.user_level < mt.user_level
)
DEMO
Finally I found solution:
select c.id companyid, c.title company_title, m.firstname member_name, mt.user_level
from company c
inner join company_member cm on cm.companyid = c.id
inner join member m on m.id = cm.memberid
inner join member_type mt on mt.id = m.member_type_id
inner join
(select c1.id companyid, mt1.user_level
from company c1
join company_member cm1 on cm1.companyid = c1.id
join member m1 on m1.id = cm1.memberid
join member_type mt1 on mt1.id = m1.member_type_id
group by c1.id,m1.id
order by user_level asc
) sq on c.id = sq.companyid and sq.user_level = mt.user_level
group by c.id;
Correct this, if anyone have better solution or simplified solution.
Check this SQL Fiddle
i want to join 2 mysql tables .but join information is in a separate table .let's say i have 3 table named student ,course and reg contain id of student and course he does.
student table
s_id | name
1 | miki
2 | foly
3 | oski
course table
c_id | name
101 | c++
102 | java
103 | ruby
reg table
s_id | c_id
1 | 101
1 | 102
2 | 101
now i want to get all the course someone do.i wrote sql query for that without using join query .but i want to do same thing using join query .this is my query
SELECT c.name FROM student as s,course as c,reg as r where r.s_id=s.s_id and r.c_id=c.c_id and s.name='miki';
Statement
SELECT c.name
FROM student as s,couse as c,reg as r
where r.s_id=s.s_id and r.c_id=c.c_id and s.name='miki'
is join too, , between table names is short cut for cross join, so you already using joins (actually you have some conditions in where, so RDBMS will optimize it to inner join)
but, of course you can rewrite it to different syntax:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id and r.s_id=(select s_id from student where name='miki'));
another syntax:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id)
inner join
student as s
on (r.s_id=s.s_id and s.name='miki');
and another one:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id)
inner join
student as s
on (r.s_id=s.s_id)
where s.name='miki';
depending on bunch of conditions performance of these 4 queries can be different, but results will be the same
Just join all 3 tables to get the result
select c.name
from course c
join reg r on r.c_id = c.c_id
join student s on s.s_id = r.s_id
where s.name = 'miki'
SELECT s.name,
c.name
FROM student s
JOIN reg r
ON r.s_id = s.s_id
JOIN course c
ON c.c_id = r.c_id
WHERE s.name = "miki"
SELECT c.name, s.name as stud FROM course c
JOIN reg r on c.c_id = r.c_id
JOIN student s on r.s_id = s.s_id
WHERE s.name = 'miki'
A table
--------------------
id bId txnVolume
1 1(b table) 10.00
2 1 5.00
3 2 7.00
4 3 2.00
B table
--------------------
id cId
1 1(C table)
2 2
3 3
C table
--------------------
id cusId prodId
1 1 1
2 1 2
3 1 1
4 1 2
I want to get the sum of txnVolume on table A if C table is custId 1 and prodId 1? Thanks for the help guys! Cheers!
Select sum(coalesce(txnVolume,0))
FROM A INNER JOIN B on A.BID = B.Bid
INNER JOIN C on C.ID = B.CID
WHERE C.CustID = 1 and C.ProdID = 1
Try the following:
select sum(txnVolume) from A
join B on A.bID = B.id
join C on C.cID = C.id
where prodID = 1 and custID = 1
In sql you need to join the tables. When tables are joined they create one big table. You can then add where clauses to the columns.
SELECT sum(txnVolume)
FROM A
JOIN B on A.bId = B.id
JOIN C on B.cID = c.id
where c.custid = 1 and c.prodid = 1
SELECT SUM(a.txnVolume)
FROM c
LEFT JOIN b
ON c.id=b.cId
LEFT JOIN a
ON b.id=a.bId
WHERE c.cusId=1 AND c.prodId=1;
SELECT SUM(a.txnVolume) totals
FROM tableA a
INNER JOIN tablB b
ON a.bid = b.id
INNER JOIN tableC c
ON b.cid = c.ID
WHERE c.custID = 1 AND c.prodID = 1
I'm trying to get names of users from a voting table, I can't quite figure out how to get both the voter's name and the votee's name.
Simplified Votes Table
ID voteeID voterID
1 1 2
2 1 3
3 1 4
4 2 1
5 2 3
6 2 4
Simplified User Info Table
ID username
1 Bob
2 Sam
3 Ed
4 Mark
Where I'm at:
SELECT votes.voteeID, votes.voterID, userinfo.username
FROM votes
JOIN ???
I was playing around with the join, but I'm kind of stuck. If I join on the votee's id to the userinfo id, how would I also get the voter's name or vice versa?
Database: MySQL 5
Any help or even a hint would be wonderful, thanks in advance.
try this one,
SELECT b.`username` as Votee_Name,
c.`username` as Voter_Name
FROM Votes a
INNER JOIN UserInfo b
ON a.voteeID = b.Id
INNER JOIN UserInfo c
on a.voterID = c.id
SQLFiddle Demo
How about something like
SELECT v.ID,
v.voteeID,
u1.username Votee,
v.voterID,
u2.username Voter
FROM Votes v INNER JOIN
User u1 ON v.voteeID = u1.ID INNER JOIN
User u2 ON v.voterID = u2.ID
You need to take INNER JOIN twice to get usernames for both votee and voter:
SELECT a.voteeID, a.voterID,
b.username AS votee_name, c.username AS voter_name
FROM votes a
INNER JOIN userinfo b
ON a.voteeID = b.ID
INNER JOIN userinfo c
ON a.voterID = c.ID;
SELECT V.ID as ID B.username as Votee_Name, C.username as Voter_Name
FROM Votes V
INNER JOIN UserInfo B ON V.voteeID = B.Id
INNER JOIN UserInfo C on V.voterID = C.id;