I have SQL tables for users and relationships, like
users{ID, name, email}
relationships{ID, uid, relid, type}
I want to get all "friends" ID, name and email (by current user ID) by SQL, using JOIN or whatever.
If using join is it better to JOIN the table of users and select relationships or select relationships and join users?
something like this (I invented now, i use a bit diferrent tables and code, sorry for errors)
1:
SELECT DISTINCT u.ID, u.name, u.email FROM users u JOIN relationships r ON (r.uid = 1 OR r.relid = 1) AND r.type = 1 WHERE u.ID = r.uid OR u.ID = r.relid
2:
SELECT DISTINCT u.ID, u.name, u.email FROM relationships r JOIN users u ON r.uid = u.ID OR r.relid = u.ID = 1 WHERE r.uid = u.ID OR r.related = u.ID
Edit:
I use DISTINCT becouse when I select users whoose IDs equals to r.uid or r.related, I also get the current user (uid + related) for every relation. DISTINCT should unique the users? I'm beginner and found this on stackoverflow.
So, is it better to select from users and join relations or select relations and join users?
BTW: I quite like the answer with UNION
You can union the users where user is in the uid and in the relid.
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.uid
WHERE users.ID = 500
UNION
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.relid
WHERE users.ID = 500
Not sure why you are using DISTINCT but it looks like you just want to select all the rows from the relationships table (the friends), and then join their details from the users table.
SELECT u.id, u.name, u.email
FROM relationships r
LEFT JOIN users u ON u.id = r.uid
WHERE r.uid = 12345
Related
I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.
I am working on social networking site.
I've three tables one is user table which is used to store user details, another table is follow table which is used for followers following list.
In this table I am storing user_id and follower_id.
Third table is user_friends in this I'm storing user_id and friend_userid.
I want to search the user from my friends list and follow list. For this i've written query like this:-
select f.follower_id,uf.friend_userid,u.user_id,u.first_name,u.last_name from tbl_user u
LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id
LEFT JOIN tbl_follow f ON f.follower_id = u.user_id
where uf.friend_userid != '11'
AND u.first_name LIKE '%a%'
This query returning users only who are friends it is not returning the follow users.
Any help will be appreciated. Thanks in Advance.
You're joining tbl_follow on the Follower ID being equal to the User ID. I suspect that's probably not right.
If you don't already have one you'll need a user id key in the follower table to join on, then you can change your join to;
LEFT JOIN tbl_follow f ON f.userid = u.user_id
i've done this by using following query:-
select u.user_id,u.first_name,u.last_name from tbl_user u LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id LEFT JOIN tbl_follow f1 ON f1.follower_id = u.user_id LEFT JOIN tbl_follow f2 ON f2.user_id = u.user_id where (uf.user_id = '11' OR f1.user_id = '11' OR f2.follower_id = '11') AND (u.first_name LIKE '%s%' OR u.last_name LIKE '%s%') AND u.status = '0' group by u.first_name
This query returning me all the users who are my followers, friends and to whom i am following.
This can't be too hard, but I don't know what the term is I'm looking for. I'm sure you guys can help me out. :)
I have a table tasks with rows that have a column project_id. Each project_id refers to (the id of) a row in the projects table. Each project belongs to a certain user which is why it has a column user_id.
I now want to select all tasks from this table where the project_id equals any project of a certain user.
Or put more simply:
Each TASK belongs to a PROJECT which belongs to a USER.
I want to create a SELECT-statement to receive all TASKS that belong to a specific USER. The only link between each TASK and a USER is through the PROJECT.
How do I accomplish this?
Thank you very much for your help! :)
JOIN the tables:
SELECT
t.*
FROM tasks t
INNER JOIN projects p ON t.project_id = p.project_id
INNER JOIN users u ON p.user_id = u.user_id
WHERE u.user_id = #AcertianUserId
SELECT
tasks.*
FROM
users
INNER JOIN
projects ON users.id = projects.user_id
INNER JOIN
tasks ON projects.id = tasks.project_id
WHERE
users.id = 1
SQL Fiddle
select t.*
from tasks t
join projects p on p.id = t.project_id
join users u on u.id = p.user_id
where u.id = 17
Try :
SELECT t.*
FROM user u, projet p, task t
WHERE u.id = p.user_id
AND p.id = t.project_id
AND u.id = your_id
Or (same result) :
SELECT t.*
FROM user u
INNER JOIN projet p ON u.id = p.user_id
INNER JOIN task t ON p.id = t.project_id
WHERE u.id = your_id
I have these two tables:
1. users
-id
-name
-email
-rank
2. pages
-user
-id
I need to select all the data from users and count how many pages did he wrote (from pages user is the id of the user from users table).
Can you make it into 1 complex query?
select u.*,count(p.*) from users u left join pages p on u.id=p.user group by u.id;
I can't make it into a complex query, but here's a simple one:
SELECT u.name, u.email, u.rank, u.id, count(p.id) as 'PageCount'
FROM Users u
JOIN Pages p ON
p.id = u.id
GROUP BY u.name, u.email, u.rank, u.id
Yes, do it like this:
select u.id, u.name, u.email, u.rank, count(p.id) as 'counter' from users u
inner join pages p on p.user = u.id
group by u.id, u.name, u.email, u.rank
You group by the users info and calculate (count) the number of pages for each user.
How about this.
select u.id, count(p.id) as 'number_of_pages_wrote'
from users u inner join pages p
on u.id = p.id
group by u.id;
You can do it like this -
SELECT u.*, p.count
FROM users u,
(SELECT pa.id, count(1) FROM pages pa WHERE pa.id = u.id) p
WHERE u.id = p.id
What about this simple query
SELECT user, COUNT(*) AS pages FROM page GROUP BY user
select users.id, users.name,users.email,users.rank, count(pages.id)
from users,pages
where users.id = pages.user
group by users.id
This can be accomplished by a JOIN.
For instance,
SELECT * FROM pages
INNER JOIN users ON (users.id=pages.user)
WHERE users.id=5
will select all pages from userid 5. I'll let you do the rest to include the aggregate function ;)
SELECT *
FROM users
LEFT JOIN payments ON payments.user_id = users.user_id
WHERE payments.id = (SELECT max(p2.id)
FROM payments p2
WHERE payments.user_id = p2.user_id)
Is the query I am currently using to select the user info, as well as their last transaction. The problem is that I'd like to select the users even if they do not have a transaction in the database, how do I go about this?
My table structure I'm playing with is very simple:
USERS - user_id, name
PAYMENTS - id, user_id, method
You might be better off (performance wise) joining onto a sub query like this:
SELECT *
FROM users u
LEFT JOIN
(Select user_id, max(id) from payments group by user_id) p
ON p.user_id = u.user_id
By putting any conditions in the Where clause, you are turning it into an inner join,
try something like this:
SELECT users.*, payments.*
FROM users
LEFT JOIN payments ON payments.user_id = users.user_id
AND payments.id = (SELECT max(p2.id)
FROM payments p2
WHERE payments.user_id = p2.user_id)
You have two columns "user_id" in the result set, one from users table, and one from payments. The payments.user_id will be null, but users.user_id should have value.
Why is everybody doing all that crazy stuff?
select u.user_id u_uid, u.name, p.user_id p_uid, p.method from users u left join payments p on (u.user_id = p.user_id);