Here is my table structure (fun_users)
id first_name last_name email passkey place profession self_des profile_img user_type active last_login reg_date reported_banned
And my friendship table is
id user_id friend_id status date_request from_ip
And here is the query im using to fetch details of logged in user friend
SELECT `fun_friends`.`id` as fid, `fun_users`.`id` as uid, `fun_users`.`first_name`, `fun_users`.`profile_img`, `fun_users`.`profession`, `fun_users`.`place` FROM (`fun_friends`) JOIN `fun_users` ON `fun_users`.`id`=`fun_friends`.`friend_id` WHERE (`fun_friends`.user_id= '".$_SESSION['user_row_id']."' AND `fun_friends`.`status` =1) OR (`fun_friends`.friend_id= '".$_SESSION['user_row_id']."' AND `fun_friends`.`status` =1)
The result is
fid uid first_name profile_img profession place
11 47 Agnii thumbs/2013-03-311364721555.jpg Software engineer somewhere
The query returns the details of loggedin user not his friend details. can anyone help me ?
The query below uses a subquery which gets all the friends for specific user. Table fun_users is joined against the subquery twice because there are two columns on the subquery which are dependent on it.
SELECT a.id AS FID,
IF(a.user_ID = 'user_row_id_HERE', c.id, b.id) AS UID,
IF(a.user_ID = 'user_row_id_HERE', c.first_name, b.first_name) AS first_name,
IF(a.user_ID = 'user_row_id_HERE', c.last_name, b.last_name) AS last_name,
IF(a.user_ID = 'user_row_id_HERE', c.profile_img, b.profile_img) AS profile_img,
IF(a.user_ID = 'user_row_id_HERE', c.profession, b.profession) AS profession,
IF(a.user_ID = 'user_row_id_HERE', c.place, b.place) AS place
FROM
(
SELECT id, user_ID, friend_ID
FROM friendship
WHERE 'user_row_id_HERE' IN (user_ID, friend_ID) AND
status = 1
) a
INNER JOIN fun_users b
ON a.user_ID = b.id
INNER JOIN fun_users c
ON a.friend_ID = c.ID
So the question arises, what happens on this line?
IF(a.user_ID = 'user_row_id_HERE', c.id, b.id) AS UID
Basically, it test for the value of the user_ID from the subquery if it is equal to the current user. If it happens to be equal, the column from table fun_users c will be returned and vice versa.
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
Related
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
I have two table users and album. In users table there is user_id primary key .In other table albums there are multiple rows with that user_id because every time when a user upload a new album it uploads with user_id as foreign key. I want to select only once the user_id with other table(album) ignore other result set.
How can I achieve this?
SELECT a.*, b.*
FROM users a
INNER JOIN album b
ON a.user_ID = b.user_ID
INNER JOIN
(
SELECT user_ID, MAX(photo_id) max_rec
FROM album
GROUP BY user_ID
) c ON b.user_ID = c.user_ID AND
b.photo_id = c.max_rec
SELECT album.* FROM album LEFT JOIN users ON user.id = album.id WHERE user.id = SOMEIDHERE
I believe this will work, your not giving me a whole lot of info to work with.
SELECT *
FROM ( SELECT u.*, a.*
FROM users AS u
INNER JOIN album AS a
ON u.user_ID = a.user_ID
ORDER BY a.created DESC) AS h
GROUP BY user_ID
ORDER BY b.created DESC -> ORDER BY whatever row you wish for. In this case the newest one is chosen.
I have about six(6) tables each linked with userid. One of the tables is userinfo. The user info contains user details including their store platform(eg magento)
Userinfo contains both active and non-active users (active users have created at least one activity in the other 5 tables).
I want to count distinct number of users in the userinfo with platform of magento who have records in any of the other tables.
Currently I am able to count distinct number of users in the other five tables with the ff code but want to join this with the userinfo table so I can select active users with platform magento.
Without adding the userinfo table means I have no way of selecting users by platform.
Selecting users in userinfo table only, with platform of magento will be easy, but that means I may select users who only register but do not go on to create activity on my app.
$query3 = ("SELECT COUNT(*)
FROM (
SELECT userid FROM table1
UNION SELECT userid FROM table2
UNION SELECT userid FROM table3
UNION SELECT userid FROM table4
UNION SELECT userid FROM table5
) AS UserIDs");
$result3 = mysql_query($query3) or die(mysql_error());
$row3 = mysql_fetch_row($result3);
echo "Number of distinct users in all tables = ".$row3[0] ."<br />";
**Table 1**
Id userid name adresss
**Table 2**
Id Title Sex userid
**Table 3**
Id userid amount
**Table 4**
Id price promotion userid productid
**Table 5**
Id userid category tax weight
**userinfo**
Id userid username password platform
Expanding on the UNION subselect from my other suggestion, you can JOIN this with the UserInfo table and get your distinct count.
SELECT COUNT (DISTINCT ui.UserID))
FROM (
SELECT UserID FROM Table1
UNION SELECT UserID FROM Table2
UNION SELECT UserID FROM Table3
UNION SELECT UserID FROM Table4
UNION SELECT UserID FROM Table5
) AS id
INNER JOIN UserInfo ui ON ui.UserID = id.UserID
WHERE ui.Platform = 'Magento'
I would like that :
SELECT COUNT(DISTINCT ui.userid) as number
FROM userinfo ui
INNER JOIN table1 t1 ON (t1.userid = ui.userid)
INNER JOIN table2 t2 ON (t2.userid = ui.userid)
INNER JOIN table3 t3 ON (t3.userid = ui.userid)
INNER JOIN table4 t4 ON (t4.userid = ui.userid)
INNER JOIN table5 t5 ON (t5.userid = ui.userid)
WHERE ui.platform = 'magento'
And if you do :
SELECT COUNT(DISTINCT ui.userid) as number
FROM userinfo ui, table1 t1, table2 t2, table3 t3, table4 t4, table5 t5
WHERE ui.platform = 'magento'
AND t1.userid = ui.userid
AND t2.userid = ui.userid
AND t3.userid = ui.userid
AND t4.userid = ui.userid
AND t5.userid = ui.userid
If it doesn't work, try to replace SELECT COUNT(DISTINCT ui.userid) as number by SELECT ui.* for see.
I have an application, very basic description:
Users login -> they post "activities" (like snowboarding)
Users have friends
Users can bind multiple friends to activities
Users can "like" activities created by friends
What I need is a query to check if the user is allowed to "like" an activity by one of their friends. They are only allowed when one of the users friends was bound to the target activity.
users: id, name
usersFriends: id, uid, friendUid
activities: id, description
activitiesUsers: id, activityId, uid
activitiesLikes: id, activityId, uid
I hope someone can help me with this query, and if possible to return true or false. I hope my question is clear and thanks for your time :)
This should produce a list of activities.id for all of the user's friends.
SELECT
activities.id AS canLikeId
FROM
users u
JOIN usersFriends uf ON u.id = uf.uid
JOIN activitiesUsers au ON uf.friendId = au.uid
JOIN activities a ON a.id = au.activityId
Wrapped in an EXISTS, it looks like:
SELECT activities.id FROM activities aCanLike
WHERE EXISTS (
SELECT
a.id AS canLikeId
FROM
users u
JOIN usersFriends uf ON u.id = uf.uid
JOIN activitiesUsers au ON uf.friendId = au.uid
JOIN activities a ON a.id = au.activityId
WHERE a.id = aCanLike.id
)
Or something with an IN() clause that attempts to get everything from activities owned by any of the user's friends.
SELECT
activities.*
FROM activities JOIN activitiesUsers ON activities.id = activitiesUsers.activityId
WHERE activitiesUsers.uid IN (
SELECT friendUid FROM usersFriends WHERE uid = $userid
)
Assuming you know the user's id and activity id at the time of the query, you could do something like:
SELECT COUNT(*) FROM activitiesUsers WHERE activityId = 'xx' AND uid IN (SELECT friendUid FROM usersFriends WHERE uid = 'xx')
Should return 0 if none of their friends are bound to the activity or a positive number if they have friends bound to that activity ...
This should do it:
select
count(*) as CanLike -- 0 if false, >= 1 if true
from
usersFriends uf
join
activitiesUsers au
on
uf.friendUid = au.uid
where
au.id = $activityId
and
uf.uid = $userId
SELECT COUNT(*) AS allowed
FROM userFriends
JOIN activitiesUsers ON (userFriends.uid = activitiesUsers.uid
AND activitiesUsers.activityId = $activity_id)
WHERE userFriends.uid = $user_id
I am struggling with a MYSQL query - I have 2 tables :
Table 1 (info) containing UID, first_name, last_name.
Table 2 (card) containing UID, pic .
What I am trying to do is get all results into an array:
WHERE UID IN '$ids' AND LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC
I figured an INNER JOIN so my current code is:
("SELECT UID, first_name, last_name, pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM info
INNER JOIN card ON info.UID=card.UID)
WHERE LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC")
This is producing the following error though:
'Every derived table must have it's own alias'.
Am I going about this the right way with inner join, and how do I give the derived table an alias? Thanks in advance!
select b.UID, g.first_name, g.last_name, b.pic
from user_data.general_info g
inner join user_data.Bcards b on g.UID = b.UID
where LEFT(g.last_name, 1) = '$letter'
order by g.last_name, g.first_name asc
The inner query should be named.
SELECT users.UID, users.first_name, users.last_name, users.pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM user_data.general_info
INNER JOIN user_data.Bcards ON general_info.UID=Bcards.UID) users
WHERE LEFT(users.last_name,1) = '$letter' ORDER BY users.last_name, users.first_name ASC