I have looked at the examples and I think that the word Login is the problem. Is this an internal command word in SQL? Here is what I am trying to do.
Table 1 is Login and has LoginID and LoginName
Table 2 is LoginGroup and has LoginID and GroupID
I want to join the two so that I can get a list of LoginNames for a specific GroupID.
I have tried many different iterations and solutions on this and other sites, but cant seem to find the right combination. Here is my most recent attempt:
SELECT Login.LoginName AS login_name
FROM Login
INNER JOIN login on login.loginid = LoginGroup.LoginID
WHERE LoginGroup.LoginID = 9230
This produces:
Msg 1013, Level 16, State 1, Line 1
The objects "login" and "Login" in the FROM clause have the same exposed names. Use correlation names to distinguish them.
Can someone tell me what this needs to look like?
on your inner join you need to do:
FROM Login
INNER JOIN LoginGroup on Login.LoginID = LoginGroup.LoginID
you join tables two tables and then specify what columns in the tables to match on. It appears you were trying to join table straight to column.
Related
I have 3 tables (User, User_usergroups , usergroup). I would like to get a list of user who has usergroup equal to "member" and other groups (group_concat) belonging to this user. How do I do that?
I got the first part of the query, but I could not get other groups, aggregated groups of this user (I like to use group_concat to concatenate other groups in one field.
SELECT user.userId, (group_concat(group_.name)???)
FROM User_ user join Users_UserGroups userGroups
on user.userId= userGroups.userId
join UserGroup group_ on userGroups.userGroupId = group_.userGroupId
WHERE group_.name="member";
Assume that is the 3 tables
The outcome will be
Many thanks for your great help.
It looks like my query is so complex and it has down vote from the administrator or someone. I wish whoever did this could add their comment to my query so that, in the future, I will know what I need to do to clarify my question to the community or make my question better. Here is the answer to my question.
First retrieve the list of userId.
Then join this "member" table with other tables to find the result from the list of retrieved ID (step 1).
As we aggregate the name of the groups, we need to use groupby.
Keywords to resolve this problem is to use an alias for subquery or nested SQL
select member.userid, member.screenName, group_concat(member.name)
from (SELECT User_.userid userid, User_.screenName screenName , UserGroup.name name
FROM UserGroup
JOIN Users_UserGroups
ON UserGroup.userGroupId = Users_UserGroups.usergroupid
JOIN User_
on Users_UserGroups.userId = User_.userId
WHERE UserGroup.name = "member")member
JOIN Users_UserGroups
ON member.userid = Users_UserGroups.userId
JOIN UserGroup
on UserGroup.userGroupId=Users_UserGroups.userGroupId
GROUP BY member.userid;
I have a database with 2 tables in it one is 'enlistments' and the other one is 'users'. In the enlistments table I have a user_id and in the users table I have a name. I want to get the name of the user which belongs to the id.
I know I need to do this with an inner join like this:
SELECT enlistments.round_id, users.name
FROM enlistments
INNER JOIN users
ON enlistments.user_id=users.name
WHERE enlistments.activity_id = 1;
However I get this error: Warning: #1292 Truncated incorrect DOUBLE value
I did some research and found out it has to do with comparing an int with a string but I don't know how to solve the problem.
This is how my database looks like
join on is the condition you use to join the tables. Here it's enlistments.user_id=users.id.
select e.round_id
,u.name
from enlistments e join users u on u.id = e.user_id
where activity_id = 1
round_id
name
1
test2
Fiddle
To validate and be sure you are pulling back the exact data desired, I usually provide aliases for each column brought back and make sure to bring back the join columns also. It's good practice to label where the columns returned originated.
SELECT
Enlistments.UserID as Enlistments_UserID,
Users.ID as Users_ID,
enlistments.round_id as Enlistments_RoundID,
users.name as Users_Name
FROM enlistments
INNER JOIN users
ON enlistments.user_id=users.id
WHERE enlistments.activity_id = 1;
SELECT EN.round_id, US.name
FROM enlistments EN
INNER JOIN users US
ON US.name= CAST(EN.user_id AS VARCHAR)
WHERE EN.activity_id = 1
What you are needing is the function cast that can convert any kind of data into another, so you'll pass your integer value as the first argument followed by "AS '%DATATYPE'" where %DATATYPE is the kind of data you want to achieve.
In your case:
SELECT CAST(123456 AS VARCHAR)
-- RETURNS : '123456'
Anyway, I’m not sure that you can be able to join these two tables with the join you are using.
For more help please share some data.
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 got a few tables which I need to query to crosscheck information.
All tables are in the same database.
First table is named Confirmedsitesv2 and contains a column named sites.
Sedond table is named Standardwithmail and contains three columns. sites, username and mail.
Third table named CDDump contains alot of columns but there is only three I am intrested in. Uid, employeenddate and company.
I need to query so I get site, username and mail based on the following criteria.
Site from standardwithmail matches site in confirmedsitesv2, username from standardwithmail matches uid in CDDUmp, while the employmentenddate = 0 and company is not like %test%. Mail should not be matched with anything as it only occurs in standardwithmail but will show for all the hits from the query.
So I tryed to get my head around this with left joins for nearly two hours.
Anyone experienced who can who can explain how I should use join in this situation? I done outer joins before for totaly different case and that worked easy and fine, but can't get my head around this even after alot of time on google.
If needed I can create some fake tables and upload pictures as I can't show the real data here.
Please try this:
SELECT cfrsites.sites standard.username,
standard.mail
FROM Confirmedsitesv2 cfrsites,
Standardwithmail standard,
CDDump cd
where cfrsites.sites = standard.site
and standard.username = cd.Uid
If you can edit your post, add the Table structure and add some insert statements that will be helpful
So why mail did not work was because it's in the the big table I only used for other information, I renamed that column in standardwithmail and I ran:
select site, username, mailaddress
from Standardwithmail
left join CDDump on CDDump.uid = dbo.Standardwithmail.Username where Site in (select Site from Confirmedsitesv2) and employmentEnd like '0' and companyName not like '%test%'
order by Site;
Try out the following query. You don't need outer join or left join, but just inner joins since you only want matching records.
SELECT swm.username,
swm.mail
FROM standardwithmail swm
INNER JOIN confirmedsitesv2 csv2
ON swm.sites = csv2.sites
INNER JOIN cddump cdd
ON swm.username = cdd.uid
WHERE cdd.employmentenddate = 0
AND
cdd.company NOT LIKE '‰test‰'
I have one database that contains all of user information including name. Then there is a second database that contains notes from the users and it contains the #id but not the name. The query i am doing to retrieve user notes doesn't have name so all its doing is showing the notes, then right under it i am doing another query to retrieve the name from the first database using the common #id. But it won't show.
Is there a way I can do this query in one? Please help. Thanks.
Use:
SELECT u.name,
n.*
FROM DB2.NOTES n
LEFT JOIN DB1.USERS u ON n.id = u.id
ORDER BY u.name
Assuming the connection credentials has access to both databases, you prefix the database name in front of the table name and separate with a period.
The LEFT JOIN will show both users, and notes without users associated. Here's a good primer on JOINs.
You might need to show your code, but you can write queries against two databases (or schemas) on the same host, just qualify the table names with the database name, e.g.
SELECT db1.user.id, db1.user.name, db2.userinfo.notes
FROM db1.user
INNER JOIN db2.userinfo ON(db1.user.id=db2.userinfo.id)
The credentials you are connecting with must have access to both databases for this to work of course.