I have three tables
user_table:
user_id
user_name
leave_type:
leave_id
leave_name
leave_taken
id
leave_id
applied_by (user_id)
approved_by (user_id)
My end result should be
Result:
leave_name
applied_by (user_name)
approved_by (user_name)
This is what I have tried and got stuck at. I'm sorry for not providing what I tried the first time I posted this question.
Option 1:
SELECT leave_type.leave_name, users.user_name as applied_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
from leaves
join leave_type on leaves.leave_type = leave_type.id
join users on leaves.applied_by = users.id
Option 2:
SELECT leave_type.leave_name, users.user_name as applied_by,
leaves.approved_by, leaves.no_of_days, leaves.leave_date,
leaves.leave_upto_date, leaves.leave_status, leaves.approved_on
FROM leave_type, leaves, users
WHERE leave_type.id = leaves.leave_type
AND leaves.applied_by = users.id
P.S. I'm new to MySQL & I'm not sure how to implement this.
I achieved your desired result with the following query. I'm not very sure if this is the best way though.
SELECT leave_type.leave_name, users.user_name AS applied_by,
(SELECT user_name FROM users WHERE id = leaves.approved_by) AS approved_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
FROM leaves
JOIN leave_type on leaves.leave_type = leave_type.id
JOIN users on leaves.applied_by = users.id;
If you need to join the same table twice, you can provide it with an alias.
In this case, the user table has been joined with two different aliases. I've made the aliases in capitals, so it's easier to find them, but of course, you can write them however you want.
SELECT
leave_type.leave_name,
APPLY_USER.user_name as applied_by,
APPROVE_USER.user_name as approved_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
from leaves
join leave_type on leaves.leave_type = leave_type.id
join users AS APPLY_USER on leaves.applied_by = APPLY_USER.id
join users AS APPROVE_USER on leaves.applied_by = APPROVE_USER.id
Related
SELECT stock_transfer_cnf_ord.order_id, stock_transfer_cnf_ord.retailer_user_name, stock_transfer_cnf_ord.boy_user_name, stock_transfer_cnf_ord.order_status, stock_transfer_cnf_ord.order_on, users.address
FROM stock_transfer_cnf_ord JOIN
users
ON stock_transfer_cnf_ord.boy_user_name = 'manish' and role='courier'
when i run this query i get repeated data. actually i want the address from user where role is retailer
here is my two table users and stock_transfer_cnf_ord
You seem to be missing a JOIN condition between the tables:
SELECT st.order_id, st.retailer_user_name, stock_transfer_cnf_ord.boy_user_name, st.order_status, st.order_on, u.address
FROM stock_transfer_cnf_ord st JOIN
users u
ON st.boy_username = u.username
--------^ the join condition references both tables
WHERE st.boy_user_name = 'manish' and u.role = 'retailer';
I also added table aliases -- they make the query easier to write and to read.
Now I want to get a comments list, and I have two table named TB_COMMENT, TB_USER;
the TB_COMMENT table has three fields: WORK_ID, USER_ID, ATED_USER_ID;
the TB_USER table has three fields: USER_ID, NICKNAME.
now the front end gives me a workId, and I need to return the list include:
userId, nickname, atedUserId, atedNickname.(and the atedUserId, atedNickname may not exist).
And I just write this sql sentence:
SELECT DISTINCT TB_USER.USER_ID, TB_USER.NICKNAME, TB_COMMENT.ATED_USER_ID
FROM TB_USER, TB_COMMENT
WHERE TB_COMMENT.WORK_ID = #{workId} AND TB_COMMENT.USER_ID = TB_USER.USER_ID`
and I don't know how to get the atedNickname. Hope someone can help me, thanks.
You need one more join with TB_USER on ATED_USER_ID foreign key
SELECT DISTINCT ua.USER_ID, ua.NICKNAME, ub.USER_ID, ub.NICKNAME
FROM TB_USER ua INNER JOIN TB_COMMENT c ON ua.USER_ID = c.USER_ID
LEFT JOIN TB_USER ub ON ub.USER_ID = c.ATED_USER_ID
WHERE c.WORK_ID = #{workId}
hi i'm just new to sql and i have a hard time deleting records. i need to Delete all the records from subjcode table where the teacher is “MARSHALL”. i used this query but i doesn't work:
delete
from subjcode
where (
select sa.sno
from subjcode sa,
teacher,
course
where teacher.tname = 'MARSHALL'
and teacher.tno = course.tno
and course.cno = sa.cno
) = subjcode.sno;
and there is the table and its columns:
subjcode: sno,cno,score
course: cno,tno,cname
teacher:tno,tname
i know that all i need is just the tname,tno, and the cno but i don't know the proper query. please help me thanks
You can delete using join:
delete s
from subjcode s
join course c on s.cno = c.cno
join teacher t on t.tno = c.tno
where t.tname = 'MARSHALL';
See this for reference:
https://dev.mysql.com/doc/refman/5.7/en/delete.html
Try this:
delete s
from subjcode s
join course c on s.cno = c.cno
join teacher t on c.tno = t.tno
where t.tname = 'MARSHALL';
There is a table users with fields fbid and fcmtoken. Then there is a table friends with fields fbid and friendfbid. So in friends table, to get all of my friends, it would have to be done like this(just to help you get the idea):
SELECT friendfbid FROM friends WHERE fbid = MYFBID
I need to make a query to get all fcmtokens of my friends. I made it like this and it seems to work:
SELECT
fbid,
fcmtoken
FROM
users
WHERE
EXISTS (
SELECT
friendfbid
FROM
friends
WHERE
fbid = ?
AND friendfbid = users.fbid
)
Is it efficient enough? It seems to create a lot of select queries, so that makes me think about it.
Try moving the WHERE fbid = ? into the outer query:
SELECT
fbid,
fcmtoken
FROM
users
WHERE fbid = ?
AND EXISTS (
SELECT
*
FROM
friends
WHERE
friendfbid = users.fbid
)
But a join might be more efficient:
SELECT
u.fbid,
u.fcmtoken
FROM users AS u
JOIN friends AS f
ON f.friendfbid = u.fbid
WHERE f.friendfbid = ?
SELECT a.friendfbid, b.fcmtoken from friends a, users b where a.fbid = b.fbid
I am making a user status list of the following format "A like B's XXX". A and B are both registered users and have firstname and lastname and user id. How to join the status table with the user table twice to get the names of the two users? Thank you.
SELECT "SQACTION"."TIMECREATED",
"SQWORDLIST".*,
"SUBJECT"."FIRSTNAME" subject_fn,
"SUBJECT"."LASTNAME" subject_ln,
author.firstname author_fn,
author.lastname author_ln
FROM "SQACTION"
INNER JOIN "SQWORDLIST"
ON SQACTION.ACTION = SQWORDLIST.GUID
INNER JOIN "SQUSER" SUBJECT
ON SQACTION.SUBJECT = SUBJECT.GUID
LEFT JOIN SQDOCUMENT
ON SQACTION.ENTITY = SQDOCUMENT.GUID
LEFT JOIN SQUSER AUTHOR
ON SQDOCUMENT.AUTHORID = AUTHOR.GUID
WHERE (SUBJECT.GUID = 'B4D3BF632C0C4DB3AB01C8B284069D8F')
OR (SUBJECT.GUID IN ('67882AF3FA3C4254AF9A12CA0B0AB6E4',
'6A4B52FE233444838AACFE2AFFE4D38F',
'8CA3FB9061FF4710B51F1E398D3D1917'))
ORDER BY "TIMECREATED" DESC
This is what I have tried. Thank you.
You need to include the table name twice in the FROM clause, and use an alias so you can specify which fields from each instance of the table are used in the ON statement. You didn't provide enough details in your question to give an exact example, so here is something more general.
UserTable, with ID & Name
RegTable, with UserID, and SponsorID
select ut1.name as [User],
ut2.name as [Sponsor]
from UserTable ut1
inner join RegTable rt on ut1.id = rt.userid
inner join UserTable ut2 on rt.sponsorid = ut2.id
do you mean something like, status have two field links to user table?
select user_a.first_name as user_a_first_name, user_b.first_name as user_b_first_name, status.status_name
from status
left join users as user_a on user_a.id = status.user_from_id
left join users as user_b on user_b.id = status.user_to_id