Inner join help in mysql
SELECT MsgID, Name FROM tbl_message INNER JOIN tbl_user on tbl_message.UserID = tbl_user.UserID WHERE OrgID ='1' order by MsgID
I have a message table
msgid msg userid Orgid
24 Hi 2 1
25 hsa 4 1
User table
userid Name Orgid
2开发者_如何转开发 cas 1
4 asd 1
I want to get the name from user table. I am doing inner join to get it but I am getting error. what is wrong with the query. Error is OrgID is ambiguous
You can use the given query:
SELECT message.msg, message.msgid, message.userid,
message.orgid, user.username, user.orgid
FROM user INNER JOIN message
ON user.userid = message.userid
WHERE message.orgid='1'
order by message.msgid
I hope this will help you out.
It would help if you told us what the error is. Looking at your query, two errors I can see are:
- There's a stray comma after
SELECT MsgID
which should be removed. - The
WHERE OrganisationID ='1'
part of the statement seems to reference a columnOrganisationID
which doesn't exist. Maybe change it toOrgid
?
SELECT MsgID FROM tbl_message INNER JOIN tbl_user on tbl_message.UserID = tbl_user.UserID WHERE OrgID ='1' order by MsgID
The below code will work, in your code Orgid in where condition is ambigious
SELECT
msgid
FROM
tbl_message
INNER JOIN tbl_user ON tbl_message.UserID = tbl_user.UserID
WHERE
tbl_message.Orgid = 1
AND tbl_user.Orgid = 1
ORDER BY msgid
精彩评论