Incorrect syntax error message - petapoco

I have a class User withthe definitions: ID, Name, CustomerID
and a class Customer with the definitions: ID, Name, Address
I am trying the following query but I`m having the error message, Incorrect syntax near the keyword 'user'.
What is wrong with the sql?
I am trying to write the sql
"select user.ID, user.Name, Customer.Name from User LEFT JOIN CUSTOMER on User.CustomerID = Customer.ID" using relationsExtension as
return Connection.db.FetchOneToMany<User, Customer>(user =>user.CustomerID,
"select * from user left join customer on customer.ID= user.CustomerID order by user.name asc");

lowercase user is a SQL keyword. Try like this instead:
select [User].ID, [User].Name, Customer.Name from User ...

Related

Salesforce - Bind variables only allowed in Apex code - MALFORMED_QUERY

I need an equivalent SOQL query to this SQL query:
SQL query:
SELECT id, username, (SELECT username FROM users where id = u.manager_id) as manager_username
FROM users u
I have tried
SELECT Id, Username, (SELECT Username FROM User WHERE Id=usr.ManagerId) FROM User usr
What I keep getting is
[
{
"message": "\n(SELECT Username FROM User WHERE Id=usr.ManagerId) FROM User usr WHERE\n
^\nERROR at Row:1:Column:57\nBind variables only allowed in Apex code",
"errorCode": "MALFORMED_QUERY"
}
]
Any suggestions?
SOQL doesn't support field comparison. But in your case, it's not needed. The manager is the parent record, so its fields can be accessed via dot notation. Your query can just be:
SELECT Id, Username, Manager.Id, Manager.Username FROM User

oracle sql dev, Using a subquery, List buildings where user has no interest

I am currently trying to get the database to list a buildings, BuildingNum, BuildingName and instname that have a user who has not Interest(interest.description = null).
(PK) = Primary Key
(FK) = Foreign Key
The database schema is as follows:
Building(buildingNum(PK), Description, instname, buildName, state, postcode)
User(UNum(PK), buildingNum(FK), Surname, FirstName, initials, title)
File(FileNum(PK), title)
UserAccount(FileNum(PK)(FK), UNum(PK)(FK))
Job(JobNum(PK), id, title)
Interest(JobNum(PK)(FK), UNum(PK)(FK), Description)
So far i have tried the following block of code:
select B.buildingNum, B.BuildName, B.instname
from Building B join User U
where B.deptNum = U.deptNum in (select I.Description
from interest I
where description = null);
I'm struggling with how to do this using a sub query, all i receive is an error as this doesn't work. Im not sure if i should be using the join like that or if i have added the subquery correctly. Thanks to anyone who can help.
If you want no interests, use exists:
select b.buildingNum, b.BuildName, b.instname
from Building b
where exists (select 1
from users u left join
interest i
on i.unum = u.unum
where b.deptNum = u.deptNum and
i.unum is null -- no interests
);
The subquery returns users (in a given building) that have no interests. The exists is simply saying that at least one exists.
As a note: = null is never used for comparisons. It never returns a true value. The correct syntax is is null.

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;

Problems with joining two tables in SQL

I have two tables. user(user_id, username, password, age) and comment(comment_id, comment, user_id(foreign key)).
I'm trying to get username from user, using the user_id provided in comment.
My query looks like this:
$sql = "SELECT username FROM user WHERE user_id = (SELECT user_id FROM comments)";
I'm getting null. Is my brain working poorly or is it something else I messed up?
I just want to display all comments after each other, with the username before it.
Use IN instead of "=" .
SELECT username FROM user WHERE user_id IN (SELECT user_id FROM comments);
OR you can use a proper join, something like:
SELECT username FROM user,comments WHERE user.user_id = comments.user_id
That's not a join - a join would be:
$sql = "SELECT username FROM user u JOIN comments c ON u.user_id = c.user_id";
When you use a subquery with =, the subquery must return one value. To show all related records in a related table, use JOIN instead.

Double Inner Join generates unexpected error

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];