Double Inner Join generates unexpected error - sql

In my database I have three tables:
Users: UserID (Auto Numbering), UserName, UserPassword and a few other unimportant fields.
PrivateMessages: MessageID (Auto Numbering), SenderID and a few other fields defining the message content.
MessageStatus: MessageID, ReceiverID, MessageWasRead (Boolean)
What I need is a query to which I input a user's id and I get all the private messages he has received. In addition, I also need to receive each message's sender UserName. For this I wrote the following query:
SELECT Users.*, PrivateMessages.*, MessageStatus.*
FROM PrivateMessages
INNER JOIN Users ON PrivateMessages.SenderID = Users.UserID
INNER JOIN MessageStatus ON PrivateMessages.MessageID = MessageStatus.MessageID
WHERE MessageStatus.ReceiverID=[#userid];
But for some reason when I try saving it in my Access database, I get the following error (translated to English by me, since my office is in a different language):
Syntax error (missing operator) at expression:
"PrivateMessages.SenderID = Users.UserID INNER JOIN MessageStatus ON
PrivateMessages.MessageID = MessageStatus.MessageI".
Any ideas what could cause this?
Thanks.

You need parentheses with MS Access:
SELECT Users.*, PrivateMessages.*, MessageStatus.*
FROM (PrivateMessages
INNER JOIN Users ON PrivateMessages.SenderID = Users.UserID)
INNER JOIN MessageStatus ON PrivateMessages.MessageID = MessageStatus.MessageID
WHERE MessageStatus.ReceiverID=[#userid];

Related

how do use inner join to join two usernames from a second table, once as a sender and then as a receiver; then add records from a third table in SQL?

I have three tables in sql.
transcation_table: id(pk), date_time, sender_wallet(fk), amount_sent, sender_updated_balance, receiver_wallet(fk), amount_received, receiver_updated_balance
wallet_table: id(pk), userid(fk), wallet, currency, balance
user_table: id(pk), uname
all 'id' fields are primary keys
wallet_table.userid is foreign key of user_table.id
transaction_table.sender_wallet is foreign key of wallet_table.id
transaction_table.receiver_wallet is foreign key of wallet_table.id
I am trying to ask the database to give me a new table where any of the records in transaction_table contain a sender_wallet and receiver_wallet for a particular user.
The new returned table should be like:
date_time, sender_uname, amount_sent, sender_updated_balance, receiver_uname, amount_received, receiver_updated_balance
sender_uname and receiver_uname are new columns to be created for the purpose of making a distinction between the sender and receiver username in the newly returned table.
A returned result would be something like:
2023-02-03 09:57:38, marvin381, 40.00, 360.00, hamarni242, 40.00, 440.00
I made some poor attempts at trying to receive the intended result.
I am unable to see how I can pull the uname from the wallet_table effectively twice and join to the new table.
I also am not getting close to creating the new columns 'sender_uname' and 'receiver_uname'.
I have managed to get the inner join to work getting the data but with only one uname, but not the uname twice under 'sender' and 'receiver'.
which is not even working or coming close to the result.
select t.id,
t.date_time,
su.uname as sender_uname,
t.amount_sent,
t.sender_updated_balance,
ru.uname as receiver_uname,
t.amount_received,
t.receiver_updated_balance
from transcation_table t
inner join wallet_table sw on t.sender_wallet = sw.id
inner join wallet_table rw on t.receiver_wallet = rw.id
inner join user_table su on su.id = sw.userid
inner join user_table ru on ru.id = rw.userid
where su.id = x or ru.id = x;
That x would be a parameter (or a constant) written depending on your database which you didn't specify in tags.
PS: You could also join wallet_table and user table once with a CTE but I didn't want to go that route without knowing your database.
Here is a DBFiddle sample

trouble with inner joining 2 tables

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.

Query the fields attached to an entity in SQL drupal database

Users
Screenshot users table
field_data_field_user_first_name
screenshot fields table
What query should I use to get a table like this:
Uid Email First name
1 123#123.com example
I have tried without luck:
SELECT * FROM users
LEFT JOIN field_data_field_user_first_name
ON users.uid = ield_data_field_user_first_name.entity_id
You will get same result as mentioned above by using this query
SELECT users.uid as Uid,users.mail as Email,field_data_field_user_first_name.field_user_first_name_value as FirstName FROM users
LEFT JOIN field_data_field_user_first_name
ON users.uid = ield_data_field_user_first_name.entity_id;

SQL - need a query to search messages by recipient

I have a simple message schema where a "thread" ties 2 or more users to a stream of messages. Each message belongs to a single thread. It works just like SMS messages, or Facebook messages.
Given a string (representing a name or partial name of a user), I need a query that will find all threads that match where:
the current user (userID) is a member of the thread
the name or partial name of a user is also a member of the thread
Here are my tables:
MessageThreads:
threadID
lastUpdated
MessageThreadUsers:
threadFK
userFK
Users
userID
userFirstName
userLastName
userFullName
This query gets all threads that the current user belongs to:
SELECT DISTINCT threadFK FROM MessageThreadUsers
WHERE userFK = 'usr_developer'
But how would I join each thread with all users in the thread (not including the current user) that match by name or partial name?
This should show you the other users of all threads that the 'usr_developer' key is part of.
SELECT MT.ThreadId, U.userID, U.userFullName
FROM MessageThreads MT
INNER JOIN MessageThreadUsers MTCU on (MTCU.threadFK = MT.threadID)
INNER JOIN Users CU on (MTCU.userFK = CU.userID and CU.userID = 'usr_developer')
INNER JOIN MessageThreadUsers MTU on (MTU.threadFK = MT.threadID)
INNER JOIN Users U on (MTU.userFK = U.userID and U.userID <> 'usr_developer')
WHERE U.UserFullName like '%John%' -- Do your filters here
This joins against the tables twice, first, we join to the users table to only pull back the set of threads that have a user with the id of 'usr_developer'. Then we join the resulting threads against the users table again, this time where the user id is not usr_developer. Finally we can filter the result set by the name.

MySQL: Join two tables

I'm having problems with design correct SQL query. I'm tired like a hell today, been working over 12 horus (deadline soon) and cannot find issue...
Tables:
buddies | userid | buddyid
users | id | username
Now, what I'd like to do:
Query table buddies for all user friends (when ID = userid OR ID = buddyid). Having no problems with that.
Problem comes when I try to join users table to get username, username is NULL, can't find out why.
Would you like to help me?
Here's working query (but returning empty username)
SELECT username
FROM (
`users_buddies`
)
LEFT JOIN `users` ON ( 'users.id' = 'buddyid'
OR 'users.id' = 'userid' )
WHERE `userid` =1
OR `buddyid` =1
Thanks in advance for any help. I'm more than sure it's tiny bug (caused by me) but really cannot find it. Spent over one hour on this, then decided to ask.
Regards,
Tom
think it's the quotes, try this:
SELECT username
FROM users_buddies ub
LEFT JOIN users u
ON u.id In (ub.userId, ub.buddyid)
Secondly, your Where condition doesn't make sense. If you only want one name to come up then you can restrict it to userid = 1 or buddyId = 1.. (that's the same user, whether he's a user in user_buddies, or a buddy in user_buddies)
If what you want is to find all the buddies of user with userid = 1 , then try this:
SELECT b.username
FROM users_buddies ub
LEFT JOIN users b
ON b.id = ub.buddyid
Where ub.userid = 1
or even better,
Select u.username User, b.username Buddy
From users_buddies ub
LEFT JOIN users u
ON u.id = ub.userid
LEFT JOIN users b
ON u.id = ub.buddyid
Where ub.userid = 1
Tiny bug is that you are using incorrect quotes in ON condition
change
'users.id' = 'buddyid' OR 'users.id' = 'userid'
to
`users`.`id` = `buddyid` OR `users`.`id` = `userid`
Why do you use ` character for object names?
SELECT username
FROM users_buddies
LEFT JOIN users ON ( users.id = 'buddyid'
OR users.id = 'userid' )
WHERE userid =1
OR buddyid =1
Now everyone has spotted it, oughtn't it be this?
SELECT username
FROM users_buddies LEFT JOIN
users ON (users.id = buddyid OR users.id = userid)
WHERE userid = 1 OR
buddyid = 1;
What is the idea of all these ` marks, anyway?
As other people have observed, you're using the wrong quotes.
The ' character is for quoting strings, not objects. So, you're doing a pair of string comparisons. The string 'user.id' will never equal the string 'buddyid', and ditto for your other comparison, so the query returns nothing.
For quoting objects (tables, columns, etc) you must use the backquote character `.
You are only required to quotes objects if the object is named the same as a reserved word or has non-standard characters (spaces and such) in it. So, in this query, none of the quote characters are actually required.
IMO, if you must quote an object, that's a good sign that you shouldn't use that particular name.