Sql Request? Where clause - mysql

First of all, sorry for my english, I'm from france.
I try to make a simple SQL request, but I'm stucked, and I don't know why. So here are a simplification of what I want to do:
2 tables:
user_profil: iduser , idprofil
profil : idprofil, profil
What I want:
I'd like to get the list of the profils which one user don't have. So if a user have a profil in user_profil, I don't want to have this profil in the result of the request.
Infact, if the user have the profil, I don't want this profil to be in the result of the request.
I tried:
select distinct (p.idprofil), p.profil
from profil p, user_profil u
where p.idprofil != u.idprofil and u.iduser = X
But it doesn't work (it works if a user have only one profil, but if he has at least 2 profils, it doesn't work.)

Use not in or not exists:
select u.*
from user_profil u
where u.idprofil not in (select p.idprofil from profil p);

Related

How to write Flexible Search for joined tables in Hybris

I am trying to get all users which don't have addresses, there is a relation between user and address tables(one to many) and the address has an owner attribute that refers to Users pk. how can I achieve that ??
SELECT {u:pk}
FROM {User AS u
LEFT JOIN Address AS a ON {u:pk}={a:owner}}
WHERE {u:owner} IS NULL
I wrote a query which I guess doesn't work correctly
You can try something like this :
SELECT {pk} FROM {User} WHERE {pk} not in ({{ SELECT {owner} FROM {Address} WHERE {owner} IS NOT NULL }})

Outer query value is not able use in inner query

SELECT
(select Email from Contact where AccountId = Account.Id),
Id,
BillingCity,
BillingCountry,
BillingPostalCode,
BillingState,
BillingStreet,
Name,
Phone
FROM Account
where
LastModifiedDate < #[flowVars['timestamp']]
Problem here is I am not able to get the Email which is present in the sub query based on the Id of current iteration. Can you please help on this
I'm not sure how you are running the query, and how you are accessing the result, but if you are doing it in a place that does not give you the result dynamically, meaning that you try to access the columns by expected name, i.e. trying to get the "email" column somehow, then you need to fix a small issue in the query.
You need to add the AS operator to give your subquery a meaningful name like email like so:
...
(select Email from Contact where AccountId = Account.Id) as email,
...
See fiddle here for working example: db-fiddle
You can get rid of the scalar sub-query and just put a join to the CONTACT table instead. The following assumes that the CONTACT table is an optional relationship.
SELECT
con.email,
acct.Id,
acct.BillingCity,
acct.BillingCountry,
acct.BillingPostalCode,
acct.BillingState,
acct.BillingStreet,
acct.Name,
acct.Phone
FROM account acct
LEFT OUTER JOIN
contact con ON con.account_id = acct.account_id
WHERE acct.LastModifiedDate < #[flowVars['timestamp']]

SQL Joining the correct username

Say I have the following tables
User
__________
id
username
email
FriendGame
__________
id
useroneid
usertwoid
status
I want to get games that the current user is part of, so I do this:
SELECT *
FROM FriendGame
WHERE useroneid=1663702020516206
OR usertwoid=1663702020516206
AND STATUS =1;
This is fine. Now I want to join the username, but only for the user that ISNT the supplied user (1663702020516206) since in FriendGame the given user exists EITHER as useroneid or usertwoid.
You can pretty much translate your logic directly into an on clause:
SELECT fg.*
FROM FriendGame fg JOIN
User u
ON (fg.useroneid = 1663702020516206 and fg.usertwoid = u.id) or
(fg.usertwoid = 1663702020516206 and fg.useroneid = u.id)
WHERE 1663702020516206 in (fg.useroneid, fg.usertwoid) AND
STATUS = 1;
Actually, the where clause is not necessary to get the right result set, but I think it makes the intention of the query clearer.

SQL 2 Tables join id

I think this is an easy question but i can't find a answer I don't have much time left...
help me please
I have a Table "Anime" with the columns "animeID, Name, ..." and I have the Table "anime_user2" with the columns "anime_userID, ..., users_userID, anime_idAnime(Foreign Key)"
Now I want to get all Names from Table "Anime" that are not in already in the Table "anime_user2" with the ID from the user that is logged in
I tried this but it doesn't work as I wanted :
"SELECT Name FROM Anime LEFT JOIN anime_user2 ON idAnime = anime_idAnime WHERE users_userID = $userID AND idAnime != anime_idAnime"
There is probably an easier way to solve this but I can't see it?
You can do as
select
a.Name
from
Anime a
left join anime_user2 au
on au.anime_idAnime = a.animeID
where
au.anime_idAnime is NULL
Here is a demo how it works

MySQL subquery within LIKE CONCAT

I'm trying to create a query that checks an inserted value against a field. I'm trying to accomplish this using a sub-query, but got stuck. What I'm trying to accomplish:
(message1) User1 says: Hello User2
(message2) ChatX says: User1, user2 said hello to you!
I figured that in order to accomplish that, I need a subquery within the like statement.
SELECT chat.id, chat.userid, chat.message, user.userid, user.username
FROM chat, user
WHERE LOWER(message) LIKE CONCAT('hello ', (SELECT user.username FROM user WHERE XXX = user.username))
AND chat.userid = user.userid
The XXX in the LIKE statement, is the username someone said hello to. It's there it should check against the user table and if the message matches a user output ChatX's line. My question, how do I make XXX work and how do I set it?
SELECT c.id, c.message,
sender.userid, sender.username,
receiver.userid, receiver.username
FROM chat c
JOIN user sender ON c.userid = sender.userid
JOIN user receiver ON LOWER(message) like CONCAT('hello ', receiver.username)