How to display users from two MySQL tables - php

I have two mysql tables-members and addclique. Members table contains the details of all users while addclique table is 'friends' table with two columns-adder_id, which contains id of the user that sent a friend request and clique_id, contains the id of user that accepted the friend request.
Please am trying to select the friends of a particular user but am finding hard getting the details (photo, name, etc) of the friends from the members table because I don't know at which point to join members table to the addclique table.
$sql = "SELECT i.*, m.* FROM addclique i JOIN members m ON m.id = ******** WHERE adder_id = :id OR clique_id = :id";
$result= $db->query($sql, array('id' => $_SESSION['id']);

The query below will select all members that a particular person has added plus all members that have added the same person.
select m.* from members m
join addclique a on m.id = a.adder_id
where a.clique_id = :id
union
select m.* from members m
join addclique a on m.id = a.clique_id
where a.adder_id = :id

If I'm understanding your question correctly, you need to join on m.id = a.adder_id or m.id = a.clique_id. You can do this with in:
set #id := 4;
select *
from members m
join addclique a on m.id in (a.adder_id, a.clique_id)
where #id in (a.adder_id, a.clique_id)
and m.id != #id
SQL Fiddle Demo

Related

Joining 2 fields into another table

How do i join two fields from table1 to table 2?
I have this code below, how do i also join fields "mobilenumber","firstname" and "lastname" from the user table into the user_address table?
$query = "SELECT * FROM user_address WHERE user_id IN
(SELECT id FROM user WHERE email = '".$email."')";
$query = "SELECT a.*,b.mobilenumber,b.firstname,b.lastname
FROM user_address a
left join user b on a.user_id=b.user_id
WHERE a.user_id IN
(SELECT id FROM user WHERE email = '".$email."')";
You can use LEFT JOIN instead and select attributes from both table. Example:
$query = "SELECT ua.*,
u.mobilenumber,
u.firstname,
u.lastname
FROM user_address ua
LEFT JOIN USER u
ON u.id = ua.user_id
WHERE u.email = '$email'";

group messages by same users

I have 2 tables .. 1- users,,2-messages
I wrote this query but it doesn't show the last subject and I need last subject
select (
CASE WHEN messages.sender = 68314 THEN messages.receiver ELSE messages.sender END
) AS user_id,
MAX(messages.added) last_added,messages.subject,
MAX(messages.id) as last_id,users.username
FROM messages
INNER JOIN users ON users.id = IF(messages.sender = 68314, messages.receiver, messages.sender)
WHERE (messages.sender = 68314 or messages.receiver = 68314) AND messages.sender!=0
GROUP BY
(
CASE WHEN messages.sender = 68314 THEN messages.receiver ELSE messages.sender END
)
ORDER BY last_added DESC
It shows first subject, not the last one.
please give this query a try. It first select max(id) of messages of people talking to id 68314 assuming those are created last...and joins back with messages, and the joined with users to get the other person's name.
SELECT last_messages.user_id,
m.added as last_added,
m.subject,
last_messages.last_id,
u.username
FROM messages m
INNER JOIN
(SELECT IF(sender = 68314,receiver,sender) as user_id,
MAX(id) as last_id
FROM messages
WHERE IF(sender = 68314,sender,receiver) = 68314
GROUP BY user_id
)last_messages
ON last_messages.last_id = m.id
INNER JOIN users u ON last_messages.user_id = u.id
ORDER by last_added;
sqlfiddle

How to display values from two Mysql tables with similar columns.

Good day, I selected two MySQL tables- tcomment and members, but these two tables have similar fields called 'id' and now, I want to select the id of the 'tcomment' table but instead, the id of 'members' table shows up. How do I do this? This is the code below.
<?php
$comment= "SELECT c.*, m.* FROM tcomment c JOIN members m ON c.poster = m.id WHERE comment_id = :id";
foreach ($db->query($comment, array('id' => $_GET['id'])) AS $tresult){
echo "{$tresult['id']}";
}
?>
You could use aliases on the SQL query;
Sample Code
$comment= "SELECT c.*, c.id as comment_id, m.*, m.id as member_id FROM tcomment c JOIN members m ON c.poster = m.id WHERE comment_id = :id";
As #u_mulder said, apply aliases
$comment =<<<ccc
SELECT c.id AS 'idWanted', c.*, m.*
FROM tcomment c JOIN members m ON c.poster = m.id
WHERE comment_id = :id
ccc;
foreach ($db->query($comment, array('id' => $_GET['id'])) AS $tresult){
echo "{$tresult['idWanted']}";
}

getting data from four tables with tight WHERE clause

i need help getting data from different tables and insert into other different table Here are the Queries
"SELECT commentID, date, comment, subject, parentID, aBUserID FROM comments WHERE status = 'APPROVED'"
"SELECT topicID, subForumID, aBUserID, lastPostID, views, replies, startDate FROM topic WHERE status = 'APPROVED' AND topicID = $parentid";
// $parentID need to be matched from above query parentID,
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID";
// $cmtaBUserID = aBUserID from first query
"SELECT userName FROM users WHERE aBUserID = $topicaBUserID";
//$topicaBUserID = aBUserID from second query
Last 2 queries are from same table but using different where clause
i used different inner join left join from solutions posted here but non of these worked for me stuck since last 2 weeks please help
PS data from all above Queries will be inserted to a single table i need these to be combined so i can have them all in one place
If you want to perform the operation in same query use 'OR'
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID OR aBUserID = $topicaBUserID";
Please try this
SELECT userName from users where aBUserID IN(SELECT aBUserID FROM comments WHERE status = 'APPROVED')
Couldn't test it but Maybe this is what you are looking for.
SELECT c.commentID, c.date, c.comment, c.subject, c.parentID, c.aBUserID,
t.topicID, t.subForumID, t.aBUserID, t.lastPostID, t.views, t.replies, t.startDate,
u.userName
FROM
comments c
left outer join topic t on t.topicID = c.parentID
left outer join users u on u.aBUserID = c.aBUserID and u.aBUserID = t.aBUserID
WHERE
c.status = 'APPROVED' and t.status = 'APPROVED';
try this:
SELECT
comment.[commentID],
comment.[date],
comment.[comment],
comment.[subject],
comment.[parentID],
comment.[aBUserID],
commentuser.[userName],
topic.[topicID],
topic.[subForumID],
topic.[aBUserID],
topic.[lastPostID],
topic.[views],
topic.[replies],
topic.[startDate],
topic.[userName]
FROM comments comment
LEFT OUTER JOIN users commentuser
ON commentuser.aBUserID = comment.[aBUserID]
LEFT OUTER JOIN
(
SELECT
t.[topicID],
t.[subForumID],
t.[aBUserID],
t.[lastPostID],
t.[views],
t.[replies],
t.[startDate],
u2.[userName] --user from users table joined to topics table
FROM topic t
LEFT OUTER JOIN users u
ON u.aBUserID = t.[aBUserID]
WHERE t.[status] = 'APPROVED'
) topic
ON topic.topicID = comment.parentID
WHERE comment.[status] = 'APPROVED'

SQL select from all from table that are not in other table

I'm trying to select all users that their user id is not in another table.
Users table (user id is user_id)
Ghosts table (user id is id)
this is what I got so far:
$sql = "select * from users where users.user_id <> ghosts.id;";
Please advise...
Cheers!
You can use NOT IN:
select * from users u where u.user_id not in (select id from ghosts);
or NOT EXISTS:
select *
from users u
where not exists (select 1 from ghosts g where g.id = u.user_id)
Something like this may also work, although I like Brian's answer.
SELECT *
FROM users u
LEFT JOIN ghosts g
ON u.user_id = g.id
WHERE g.id IS NULL

Categories