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
Related
I found this sentence in the code:
$dql
= <<<DQL
SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
AND
r.profile = :sales_profile
AND
r.company = :company
AND
u.onlyStatus NOT IN (:status)
DQL;
I don't know how to works that query inside NOT IN sentence, help me please.
I need to know:
What return the query inside of NOT IN, (data types, so on...)
How to works a query inside of NOT IN (is posible ?)
SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
this means take all the users that don't currently have 'objective' in the 'Goal' table.
Is this what you needed ?
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 ...
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)
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
This is my statement for me to pull all the data of my users' friends that they have added:
$q = $dbc -> prepare("SELECT a.* FROM accounts a INNER JOIN friends fr ON (a.id = fr.friend_id) WHERE fr.id = ?");
$q -> execute(array($details['id']));
Now this is basically where the id matches in the friends table, pull the friends id and all the relevant data with it.
I am trying to implement a friends online page as well, how would I also test to see if a column in accounts for the friend_id matches a certain criterion?
This is how I pull all the users that are online...
$online_users = time() - 900;
$q = $dbc -> prepare("SELECT * FROM accounts WHERE last_active > ? && id != ? ORDER BY id");
$q -> execute(array($online_users, $details['id']));
SELECT a.*
FROM accounts AS a
JOIN friends AS f ON (a.id = f.friend_id)
WHERE f.id = ?
AND a.last_active > ?
ORDER BY id
You don't need to repeat the condition on a.id since the join enforces it.