I have two queries I need in one table, and haven't been able to find results in searching.
In the first table there is a UserID, which is a number, along with what the actual user id is and user's name.
My issue is that in every other query the user is only referred to as this number. I'm needing help in how to translate this UserID value into the actual User's name in my other queries.
Table with User's ID and User's Name:
SELECT *
FROM [Table].[dbo].[User]
Table example where the user is only referred to as this number, along with all other tables:
SELECT *
FROM [Table].[LoginStatus]
You need to join the two tables:
SELECT u.[UserName], l.*
FROM [LoginStatus] l
JOIN [Users] u ON u.id = l.user_id
Related
I have two tables, Users and Relationships tables. Users table has following columns:
id, name,password,username,email,avatar,followersCount,followingCount,tweetCount.
And the Relationships table has the following columns:
id, followingId, followerId
How should I go about creating a SQL query to extract a user with a specific Id and find id's from Relationships that user is following? So in other words find people that user follows
I've come this far so long
SELECT *
FROM public."Users" JOIN
public."Relationships"
ON (public."Users".id = public."Relationships".id)
If I understand correctly, you want:
SELECT u.*
FROM public."Relationships" r JOIN
public."Users" u
ON u.id = r.followerId
WHERE r.followingId = ?;
? is a parameter placeholder for the user you care about. This returns all the followers of that user.
Do you mean this query
SELECT public."Users".*
FROM public."Users"
JOIN public."Relationships"
ON public."Users".id = public."Relationships".followingId
AND public."Relationships".followerId = a user ID
I am not really clear about followerId and followingId mean but you can change them in the query if it is not what you want.
I have two tables "users" and "emp_details". "users" are Branch mangers who have login id for monitoring their own branch's employees. "emp_details" includes employees working for different branches which include branch managers aslo.
when Branch manager ("users") log in it should fetch details from "emp_details" only if this particular "users" branch (code: location) match.
SELECT emp_details.emp_no, emp_name, designation, emp_details.location
FROM emp_details
INNER JOIN users
WHERE users.location = emp_details.location
used above code but showing data for all users created not as per the login-ed users location. Please help
From your query it will give the output of all the data which have matching locations.
If you want to get the data which belong to the location of the specific user, you have to filter that user from the query. Otherwise query will output all the matching data.
DECLARE #UserName AS Varchar(50)
SELECT ed.emp_no, ed.emp_name, ed.designation,ed.location
FROM emp_details ed
INNER JOIN users u ON u= ed.location
WHERE u.User_Name = #UserName
Please note that above parameter can be user name or user ID or any relevant thing, but you have to pass that to the sql query.
It looks like you want a WHERE clause -- after fixing your WHERE clause to be an ON clause:
SELECT ed.emp_no, ed.emp_name, ed.designation, ed.location
FROM emp_details ed JOIn
users u
ON u.location = ed.location
WHERE u.login_id = ?;
? is a parameter placeholder for the manager's login id. That seems to be how you are identifying the users.
This is task table:
This is user table:
I want to select user tasks.
I would give from backend ("given_to_user) id.
But The thing is I want that SELECTED data would have usernames instead of Id which is (created_by_user and given_to_user).
SELECTED table would look like this.
Example:
How to achieve what I want?
Or maybe I designed poorly my tables that It is difficult to select data I need? :)
task table has to id values that are foreign keys to user table.
I tried many thinks but couldn't get desired result.
You did not design poorly the tables.
In fact this is common practice to store the ids that reference columns in other tables. You just need to learn to implement joins:
SELECT
task.id, task.title, task.information, user.usename AS created_by, user2.usename AS given_to
FROM
(task INNER JOIN user ON task.created_by_user = user.id)
INNER JOIN user AS user2 ON task.created_by_user = user2.id;
Do you just want two joins?
select t.*, uc.username as created_by_username,
ug.username as given_to_username
from task t left join
users uc
on t.created_by_user = uc.id left join
users ug
on t.given_to_user = ug.id;
This uses left join in case one of the user ids is missing.
I have the following problem in Oracle 11g:
I have a table TBL_PERSON listing all users of my application and I need to find out all users of the database that are NOT mentioned in TBL_PERSON.
Count of tbl_person is 4207.
Count of dba_users/all_users is 4244. This means, that the difference of 37 users are system users not using the application.
So far so good. How do I identity the system users?
ID in table INT_PERSON is equal to USER_ID in dba_users. I expect a list of all users from dba_users table not listed in INT_PERSON. (37 rows)
I tried the following:
SELECT *
FROM dba_users
WHERE USER_ID
NOT
IN (SELECT ID
FROM LCM.TBL_INT_PERSON);
The result is 3804 rows, also showing users from INT_PERSON -> not what I expected
Then tried:
SELECT *
FROM dba_users a
WHERE USER_ID
NOT
IN (SELECT ID
FROM LCM.TBL_INT_PERSON b
where b.id = a.User_id);
Which makes no difference.
Those post do not solve my problem:
Find records in one table which does not have a matching coulmn data existing in another table
Select records from a table, which don't exist in another table
Or is it a secret of the dba_users table? Where is my mistake?
How do I identity the system users?
First verify the count of application users.
This query should return all the ID of the application users, i.e. 4207
If not - you have other problem, than you describes.
select USER_ID from dba_users
INTERSECT
select ID from TBL_INT_PERSON;
Now show the system users
select USER_ID from dba_users
MINUS
select ID from TBL_INT_PERSON;
This should return the IDs of the system user.
General note - simple subtraction of count(*) of two tables could be misleading, as there could be duplicated key and nulls in the tables. Using set operation as above is more secure.
The query I am trying to perform is
With getusers As
(Select userID from userprofspecinst_v where institutionID IN
(select institutionID, professionID from userprofspecinst_v where userID=#UserID)
and professionID IN
(select institutionID, professionID from userprofspecinst_v where userID=#UserID))
select username from user where userID IN (select userID from getusers)
Here's what I'm trying to do. Given a userID and a view which contains the userID and the ID of their institution and profession, I want to get the list of other userID's who also have the same institutionID and and professionID. Then with that list of userIDs I want to get the usernames that correspond to each userID from another table (user). The error I am getting when I try to create the procedure is, "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.". Am I taking the correct approach to how I should build this query?
The following query should do what you want to do:
SELECT u.username
FROM user AS u
INNER JOIN userprofspecinst_v AS up ON u.userID = up.userID
INNER JOIN (SELECT institutionID, professionID FROM userprofspecinst_v
WHERE userID = #userID) AS ProInsts
ON (up.institutionID = ProInsts.institutionID
AND up.professionID = ProInsts.professionID)
Effectively the crucial part is the last INNER JOIN statement - this creates a table constituting the insitutionsids and professsionids the user id belongs to. We then get all matching items in the view with the same institution id and profession id (the ON condition) and then link these back to the user table on the corresponding userids (the first JOIN).
You can either run this for each user id you are interested in, or JOIN onto the result of a query (your getusers) (it depends on what database engine you are running).
If you aren't familiar with JOIN's, Jeff Atwood's introductory post is a good starting place.
The JOIN statement effectively allows you to explot the logical links between your tables - the userId, institutionID and professionID are all examples of candidates for foreign keys - so, rather than having to constantly subquery each table and piece the results together, you can link all the tables together and filter down to the rows you want. It's usually a cleaner, more maintainable approach (although that is opinion).