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
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.
This is the table design of an application I need to build. Kindly ignore the Role Table in the image.
Is there any method by which I can select all the records in the table transfer log , but instead of showing "User No" and "Plant No" in the result , I need to show the corresponding Employee No/Username from the user table and corresponding Plant Id from Plant Table.
Try this;)
select distinct
log.LogNo, u.EmployeeNo/Username, p.PlantId, log.StartDate, log.EndDate, log.Active
from TransferLog log
left join UserTable u on u.UserNo = log.UserNo
left join PlantTable p on p.PlantNo = log.PlantNo
Use inner join
select a.*, b.username, c.pantname
from transfer_log as a
inner join User as b a.userno = b.userno
inner join plant as c a.plantno = c.plantno
I have two tables: user and photo. They look like this:
I need to perform a SELECT on the user table based on uuid, returning the url for both profile_photo and background_photo, if they exist.
These are essentially the final fields I need (the last two being JOINed from photo):
user.name, user.profile_photo_url, user.background_photo_url
WHERE user.uuid = SOME_UUID
Can somebody point me in the right direction with this statement?
SELECT user.name, photo_a.url AS profile_photo_url, photo_b.url as background_photo_url FROM user LEFT JOIN photo as photo_a ON user.profile_photo_uuid = photo_a.uuid LEFT JOIN photo as photo_b ON user.background_photo_uuid = photo_b.uuid WHERE user.uuid = SOME_ID
This should work for you
SELECT u.name,u.profile_photo_uuid,u.background_photo_url FROM user u, photo p
WHERE u.uuid = **userid** AND
( p.uuid = u.profile_photo_uuid OR p.uuid = u.background_photo_url);
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
So my database is composed of 5 tables with different columns for each one. The only column that keeps them all identified is the id. I'm trying to get the data for a specific user, but I only seem to get all users of the database instead.
This is what I have tried:
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM
MatrixUsers.UserInfo
INNER JOIN
MatrixUsers.Users
ON
UserInfo.idUserInfo = Users.idUsers = 1
INNER JOIN
MatrixUsers.ControlAccess
ON
ControlAccess.idControlAccess = UserInfo.idUserInfo = 1
INNER JOIN
MatrixUsers.Sessions
ON
Sessions.idSessions = ControlAccess.idControlAccess = 1
INNER JOIN
MatrixUsers.FilePath
ON
FilePath.idFilePath = Sessions.idSessions = 1
INNER JOIN
MatrixUsers.Operations
ON
Operations.idOperations = FilePath.idFilePath = 1;
I tried putting 1 at the end of each id to see if they matched, but I still get all the users.
I'm new to SQL and I'm only familiar with matching rows, but not choosing specific one.
Here are the columns of each table:
ControlAccess: {idControlAccess, UserName, Pass}
Sessions: {idSessions, Matrix1, Matrix2, Result}
FilePath: {idFilePath, LocationFiles}
Operations: {idOperation, Operation}
UserInfo: {idUserInfo, Age, Country, Address, ZipCode, Phone}
Use WHERE when you want specific user. for example, select user_id from table where user_id=the_specific_user_id . Follow this basic to built you complicate statement.
Just user where after all the joins
WHERE ANY_COLUMN_REFER_TO_USER_ID = YOUR_NEEDED_ID
so your full query would be like :
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM MatrixUsers.UserInfo
INNER JOIN MatrixUsers.Users
ON UserInfo.idUserInfo = Users.idUsers
INNER JOIN MatrixUsers.ControlAccess
ON ControlAccess.idControlAccess = UserInfo.idUserInfo
INNER JOIN MatrixUsers.Sessions
ON Sessions.idSessions = ControlAccess.idControlAccess
INNER JOIN MatrixUsers.FilePath
ON FilePath.idFilePath = Sessions.idSessions
INNER JOIN MatrixUsers.Operations
ON Operations.idOperations = FilePath.idFilePath
WHERE UserInfo.idUserInfo = 1