Ok, I'm stuck with table design which I'm not able to change now and I need based on username to get all his friends from friends_relationship table
What I have now:
select distinct(username) from members m
inner join members_friends f on f.id_memb_friend1 = m.id
where (f.id_memb_friend1 = 173 or f.id_memb_friend2 = 173)
members table has columns: id, username
member_friends table has columns, id, id_memb_friend1, id_memb_friend2
The query I posted will return also username of member with id 173 who is the owner and his nickname for example is Splendid.
What I want to achive is to get all the friends together with username, based on username which I provide (Splendid = id 173).
Update:
I want to achive this:
Input: username=Splendid
Action: get usernames of splendid friends (together with their ids)
Output:
members.id = 5
username = John
members.id = 15
username = Lara
members.id = 66
username = Alex
and so on...
(Updated to accommodate the new requirement expressed in comments.)
(Updated to accommodate the new requirement expressed in the updated question.)
Your question is (was) inconsistent about whether you want to search based on username or user id. Although your query uses id, I use the username per the clarified requirement. I'm also supposing that you want the friends' usernames (only). Additionally, it appears from your initial query that you want to look at relationships in both directions (match on friend1, display all friend2, AND match on friend2, display all friend1).
Here's one way to do all that:
select mm.id, mm.username
from members mm
inner join member_friends f on f.id_memb_friend1 = mm.id
inner join members m on m.id = f.id_memb_friend2
where m.username = 'Splendid'
union distinct
select mm.id, mm.username
from members mm
inner join member_friends f on f.id_memb_friend2 = mm.id
inner join members m on m.id = f.id_memb_friend1
where m.username = 'Splendid'
SELECT CASE f.id_memb_friend1 WHEN '173' THEN f.id_memb_friend2 ELSE f.id_memb_friend1 END AS MemberID, m.username FROM members_friends f inner join members m on f.id_memb_friend1 = m.id OR f.id_memb_friend2 = m.id
where (f.id_memb_friend1 = 173 or f.id_memb_friend2 = 173)
Related
I have MySql query like this:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id
WHERE u.role = 'student' AND hr.homework_id = 8
This query work just fine, but not like what I expected. I want to display all the student (users) record is displayed although they don't have a record on homework_replies that match the homework_targets is it possible? if it possible how can I accomplish it? thanks.
Move the hr.homework_id condition from WHERE to ON to get true LEFT JOIN result:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id AND hr.homework_id = 8
WHERE u.role = 'student'
(If you have it in the WHERE clause, you'll get regular INNER JOIN result.)
I have the following database where user account details are stored in the Users table, organisation details are linked to users through the 'UsersOrganisation_Map' junction table and user permissions are linked to users through the UsersSiteRoles_Map junction table. The database is MySQL
What I'm struggling with is how to get data from both the roles and org tables in a single query and have the results returned as if they're from a single table.
Here's an example script (that doesn't work) that will hopefully describe what I'm trying to achieve.
> select * from UsersOrganisation_Map, UsersSiteRoles_Map join Users on
> UsersOrganisationMap.userID = Users.userID join Organisation on
> UsersOrganisationMap.organisationID = Organisation.organisationID
> where userEmail = 'admin#test.com' AND userPassword = 'test' AND
> accountActive = 'YES' join Users on UsersSiteRoles_Map.userID =
> Users.userID join SiteRoles on UsersSiteRoles_Map.roleID =
> SiteRoles.roleID
This should be something similar to:
SELECT
u.*,
usr.*,
sr.*,
uom.*,
o.*
FROM
Users as u
JOIN UsersSiteRoles_Map as usr
ON u.userID = usr.UserId
JOIN SiteRoles as sr
ON UsersSiteRoles_Map.roleID = sr.roleID
JOIN UsersOrganisation_Map as uom
ON u.userID = uom.userID
JOIN Organisation as o
ON o.organisationID = uom.Id
WHERE
u.userEmail = 'admin#test.com'
AND u.userPassword = 'test'
AND u.accountActive = 'YES'
You just need to join all the five tables together like :
SELECT *
FROM Users u
-- join to get roles
JOIN UsersSiteRoles_Map ur ON u.id=ur.user_id
JOIN SiteRoles r ON um.role_id=r.id
-- join to get organisation
JOIN UsersOrganisation_Map uo ON u.id=uo.user_id
JOIN Organisation o ON uo.organisation_id=o.id
-- filter results
WHERE u.email=admin ...
I have two tables like below pictures :
users table :
offer_comments
offer_comments table stores comments and answer to comments.
by using below function I can get comments and answer to comments depending on $id.
function getCommentsAnItem($id){
mysql_query("SET CHARACTER SET utf8");
$result_comments = mysql_query("select e.comment as comment,m.comment as answer_to_comment
from offer_comments e
inner join offer_comments m on e.id = m.quet
where e.offer_id=$id and e.confirm=1");
$comments = array();
while($a_comment=mysql_fetch_object($result_comments)){
$comment = array(
'comment'=>$a_comment->comment,
'answer'=>$a_comment->answer_to_comment
);
array_push($comments,$comment);
}
return $comments ;
}
How do I can get name and family from users table depending on user_id in offer_comments table ?
updated:
admin is where user_id equal = 1 .
user_id 14690 is a user and leaves a comment and admin answer to him.
in the users table :
I would like to get an array of comments and answer to comments depending on $id :
"comment=>how a u ?","answer=> I am fine","comment.name=>name","comment.family=>family"
Join your user table with your current query with offer_comments aliased as m so it will show the name and family of user who belongs to m.comment or the one who added m.comment,if you want to show the user of offer_comments aliased as e then join on e.user_id
SELECT
e.comment AS `comment`,
m.comment AS answer_to_comment,
u.name,
u.family
FROM
offer_comments e
INNER JOIN offer_comments m
ON e.id = m.quet
INNER JOIN users s
ON s.id = m.user_id
WHERE e.offer_id = $id
AND e.confirm = 1
I'm working on a small social network service.
Very classically, I have 3 tables :
- table "circles_users" : all users that belong to the same "circle"
- table "friends" : friendship relation between users
- table "checkin" : is a user "checkin" somewhere or not
Here the structure of the database : http://sqlfiddle.com/#!2/27888/1
I would like to ask my database to give me :
all users from a specific "circle" with for each of them :
- the number of friends this user has in common with user id = 3
- if this user is checkin or not
Here what I'm trying to do :
SELECT a.uid,
(
SELECT COUNT(*)
FROM (SELECT IF (uid1 = 3, uid2, uid1) AS cf FROM friends WHERE (friends.uid1 = 3 OR friends.uid2 = 3 )) as b
JOIN friends ON ((friends.uid1 = b.cf AND friends.uid2 = a.uid) OR (friends.uid1 = a.uid AND friends.uid2 = b.cf))
) as common_friends,
checkin.status as checkin_status
FROM
(SELECT circles_users.uid FROM circles_users WHERE circles_users.circlename = 'circle_A') as a
LEFT JOIN checkin ON checkin.uid = a.uid
I get this error message : Unknown column 'a.uid' in 'on clause'
It has been now 2 days that I'm trying to fix this unsuccessfully.
It seems that it is not possible to reference correlation name in my subquery.
For instance, if I replace a.uid in the subquery by a specific uid (for instance let's say '4'), I don't get any error. But of course, the result is false...
Is there someone who could help me ?
That would be very nice :)
OTHER OPTION TO FOLLOW ?
Another option would be to pass the "common_friends" subquery as a join.
I tried to do something like this :
SELECT a.uid,
c.cnt as common_friends,
checkin.status as checkin_status
FROM
(SELECT circles_users.uid as uid FROM circles_users WHERE circles_users.circlename = 'circle_A') as a
LEFT JOIN
(
SELECT DISTINCT COUNT(*) as cnt
FROM (SELECT IF (uid1 = 3, uid2, uid1) AS cf FROM friends WHERE (friends.uid1 = 3 OR friends.uid2 = 3 )) as b
JOIN friends ON ((friends.uid1 = b.cf AND friends.uid2 = a.uid) OR (friends.uid1 = a.uid AND friends.uid2 = b.cf))
) as c ON 1=1
LEFT JOIN checkin ON checkin.uid = a.uid
But again : I get this error message : Unknown column 'a.uid' in 'on clause'Anyway, do you think this version would be easier to handle and would open new possibilities to resolve my problem ?
If you want to play with my queries : (thanks to #zundarz)
http://sqlfiddle.com/#!2/27888/1
Rewrote a query based on the info you give and what you are looking for.
SELECT cu.uid, COUNT(f.uid1) common_friends, c.status
FROM circles_users cu
LEFT JOIN friends f
ON (f.uid1 = cu.uid OR f.uid2 = cu.uid)
AND f.status = "on"
AND IF (f.uid1 = cu.uid, f.uid2, f.uid1) IN (
SELECT IF (uid1 = 3, uid2, uid1)
FROM friends
WHERE status = "on"
AND (friends.uid1 = 3 OR friends.uid2 = 3)
)
LEFT JOIN checkin c
ON c.uid = cu.uid AND c.status = "on"
WHERE cu.circlename = "circle_A"
GROUP BY cu.uid
Example sqlFiddle
I'm trying to do this query.
I have these three tables
projects
id
name
authors
id
id_user
fk_id project
type = author, editor
in Table I have authors who is the creator and who is the editor
I'm trying to make a query in mysql where you can see in one record
who is the author and publisher.
I do the consultation with one of the two
SELECT p.id_project,p.name_project,a.fk_id_user,a.type,u.name
FROM projects p,users u, authors a
where p.id_project = a.fk_id_project
and a.type = 'author'
and a.fk_id_user = u.id_user
the result is
id_project, name_proyect, type, user, name_user
1 , bambo, author, 145, andrea
want is to do this
id_project, name_proyect, type, user, name_user, type2, user2, name_user2
1 ,bambo , author,145, andrea, editor, 785, barack
try to do this but not worked
SELECT p.id_project,p.name_project,a.fk_id_user,a.type,u.name,a.fk_id_user as user2, a.tipo as type2
FROM
projects p, users u, authors to
where p.id_project = a.fk_id_project
and a.type = 'author'
and a.fk_id_user = u.id_user
and user2 = u.id_user
and Typo2 = 'editor'
That type of query could use or if I'm doing wrong this.
suggest using LEFT or INNER JOIN authors a ON blablabla instead of having everything in the WHERE clause. However, try this:
SELECT
p.id_proyecto,
p.nombre_proyecto,
a.tipo,
a.fkidusario,
u.Nombres,
aa.tipo as type2
aa.fkidusario as user2,
uu.Nombres as name_user2
FROM proyectos p
JOIN usuarios u
JOIN usuarios uu
LEFT JOIN responsables a ON p.id_proyecto = a.fkidproyecto AND a.tipo = 'author' AND a.fkidusario = u.id_usuario
LEFT JOIN responsables aa ON p.id_proyecto = aa.fkidproyecto AND aa.tipo = 'editor' AND aa.fkidusario = uu.id_usuario