SQL - query for searching data in both table at the same time? - sql

Suppose I have a table called UserFollows, where I keep Follow relationships ;
UserId
FollowedId
Then I have a general Users table and it is like ;
Id
Username
I use a query like the following when I want to get all followers of a user with ID 100 ;
SELECT*FROM UserFollows WHERE FollowedId = 100;
And suppose that I also want to query the following case ;
I want to search among the followers of a particular user, with a specific Username.
So, It should be like GET ME ALL OF THE USERS THAT HAS THE WORD 'hey' IN THEIR USERNAMES AMONG THOSE FOLLOWING ME
If I had Username in my UserFollows table , I'd easily write it like ;
SELECT*FROM UserFollows WHERE FollowedId = 100 AND Username LIKE '%a';
But I don't keep their Usernames in UserFollows

You need to include the Users table in the query.
SELECT uf.*
FROM UserFollows uf
inner join Users u on u.Id = uf.UserId
WHERE uf.FollowedId = 100
AND u.Username LIKE '%a';
or, more clearly
SELECT uf.*
FROM UserFollows uf
inner join Users follower on follower.Id = uf.UserId
inner join Users followed on followed.Id = uf.FollowedId
WHERE followed.UserName = 'ME'
AND follower.Username LIKE '%a';
Although "followed" may be a keyword, so you would need different aliases or you would need to handle the names appropriately.

Related

Relationships query PostgresSQL, Follow/Unfollow functionality with PostgresSQL

I have two tables, Users and Relationships tables. Users table has following columns:
id, name,password,username,email,avatar,followersCount,followingCount,tweetCount.
And the Relationships table has the following columns:
id, followingId, followerId
How should I go about creating a SQL query to extract a user with a specific Id and find id's from Relationships that user is following? So in other words find people that user follows
I've come this far so long
SELECT *
FROM public."Users" JOIN
public."Relationships"
ON (public."Users".id = public."Relationships".id)
If I understand correctly, you want:
SELECT u.*
FROM public."Relationships" r JOIN
public."Users" u
ON u.id = r.followerId
WHERE r.followingId = ?;
? is a parameter placeholder for the user you care about. This returns all the followers of that user.
Do you mean this query
SELECT public."Users".*
FROM public."Users"
JOIN public."Relationships"
ON public."Users".id = public."Relationships".followingId
AND public."Relationships".followerId = a user ID
I am not really clear about followerId and followingId mean but you can change them in the query if it is not what you want.

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;

Select a user by their username and then select data from another table using their UID

Sorry if that title is a bit convoluted... I'm spoiled by an ORM usually and my raw SQL skills are really poor, apparently.
I'm writing an application that links to a vBulletin forum. Users authenticate with their forum username, and the query for that is simple (selecting by username from the users table). The next half of it is more complex. There's also a subscriptions table that has a timestamp in it, but the primary key for these is a user id, not a username.
This is what I've worked out so far:
SELECT
forum.user.userid,
forum.user.usergroupid,
forum.user.password,
forum.user.salt,
forum.user.pmunread,
forum.subscriptionlog.expirydate
FROM
forum.user
JOIN forum.subscriptionlog
WHERE
forum.user.username LIKE 'SomeUSER'
Unfortunately this returns the entirety of the subscriptionlog table, which makes sense because there's no username field in it. Is it possible to grab the subscriptionlog row using the userid I get from forum.user.userid, or does this need to be split into two queries?
Thanks!
The issue is that you are blindly joining the two tables. You need to specify what column they are related by.
I think you want something like:
SELECT * FROM user u
INNER JOIN subscriptionlog sl ON u.id = sl.userid
WHERE u.username LIKE 'SomeUSER'
select * from user u JOIN subscriptions s ON u.id = s.id where u.username = 'someuser'
The bit in bold is what you want to add, it combines the 2 tables into one that you return results from.
try this
SELECT
forum.user.userid,
forum.user.usergroupid,
forum.user.password,
forum.user.salt,
forum.user.pmunread,
forum.subscriptionlog.expirydate
FROM
forum.user
INNER JOIN forum.subscriptionlog
ON forum.subscriptionlog.userid = forum.user.userid
WHERE
forum.user.username LIKE 'SomeUSER'

Oracle SELECT WHERE value exists or doesn't exist

I have 3 tables; CASES, USERS and USER_META. For this issue you need to know that the USER_META table has 3 columns; user_id, meta_key and meta_value
Each user is associated with many CASES and each USER is associated with many USER_META
My current query is like this
SELECT CASES.*, USERS.*, USER_META.*
FROM CASES
JOIN USERS ON USERS."user_id" = CASES."user_id"
JOIN USER_META ON USER_META_"user_id" = USERS."user_id"
The problem with this approach is that each USER has A LOT of USER_META so my result set has too many rows. How can I rewrite this query so that I can select only the USER_META where the USER_META."meta_key" is equal to a certain value yet still get the result if they do not have this USER_META."meta_key" set yet
For example:
SELECT CASES.*, USERS.*, USER_META.*
FROM CASES
JOIN USERS ON USERS."user_id" = CASES."user_id"
JOIN USER_META ON USER_META_"user_id" = USERS."user_id"
WHERE USER_META."meta_key" = 'my_key'
This would work great but not all users have a value of "my_key" in the "meta_key" column and we still need to view their CASE. For users that do not have the "meta_key" the result should just return the CASE and USER columns.
How can I rewrite this query so it gets the result for both users with this meta_key and without?
Thanks, I hope this makes sense.
I would use a LEFT JOIN
SELECT CASES.*, USERS.*, USER_META.*
FROM CASES
JOIN USERS ON USERS."user_id" = CASES."user_id"
LEFT JOIN USER_META ON USER_META."user_id" = USERS."user_id" AND USER_META."meta_key" = ?
you need to use OUTER JOIN with the table that may have no results. In Oracle, use (+) near to the field name of the join sentence with this table. This link may help you: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/queries006.htm

Sql Query - stuck with simple left join

I've been stuck with this simple Sql query for last 3 hours and my mind is drawing blank.
I have a User table that captures name user_id etc
Then I have a friends table that captures who is friend of whom and their status
User
user_id
name
Friend
friend_id
user_id
user_friend_id
status (accepeted,pending etc)
What I want to do is - when a logged in user searches for a name say "John" - display a list of all johns on the website and next to each John - do a lookup with a friends table to see if the record exists - ie if the logged in User is a friend of any of the Johns
if it exists show the current status
if it does not exist then show "Add Friend"
So should I first run a query on User table like select user_id from User where name Like 'John%' and then use those Id's and run them against Friends table.
I am not able to get it.
Thanks
You start with all users that match your criterium (I'm not sure about the exact MySQL syntax):
SELECT u.user_id
FROM User u
WHERE u.name LIKE '%John%'
This gives you all target users. Next you want an outer join on the Friend table where you restrict the results to the currently logged in user:
SELECT u.user_id, f.status
FROM user u
LEFT OUTER JOIN Friend f ON f.friend_id = u.user_id
WHERE f.user_id = <currentUserId> AND u.name LIKE '%John%'
What you get is a result set that has the user ids of all users named John and a status flag that is either null or an actual status. If it is null, you have a potential new friend, if not, you have an existing friend.
Something like this might get you started.
COALESCE
SQL Statement
SELECT u.name, COALESCE(f.Status, 'Add Friend')
FROM User u
LEFT OUTER JOIN Friend f ON f.User_ID = u.User_ID
WHERE u.name = 'John'
Following query must solve your problem
select a.user_name,case when a.user_name like 'John%' then b.status else 'Add Friend' end case from users_det a,friend b where a.user_id = b.user_id