multiple joins in mysql query - php

I'm making a mock twitter website, and for the main feed, i'm trying to query my database for all posts, and reposts from the users that someone follows where $user_id is the person currently logged in. But it is only showing the reposted posts when i run the query. Can anyone please give me suggestions on how to fix this problem?
$query_feed = "SELECT posts.post, posts.date, users.handle, posts.id, posts.image, users.profile_pic, repost.post_id, repost.user_id
FROM posts
JOIN users ON posts.author_id = users.id
JOIN following ON " . $user_id . " = following.follower_id
JOIN repost ON posts.id = repost.post_id
WHERE (posts.author_id = following.followee_id) OR (repost.user_id = following.followee_id)
ORDER BY posts.id desc";

If you don't want the repost table to eliminate records in the result set, you'll need to use an outer join.
Also, your join condition for the following table is in the where clause, and the where clause contains your join condition. Additionally, since your join condition for following references a field in repost, the join order is incorrect.
$query_feed = "SELECT posts.post, posts.date, users.handle, posts.id, posts.image, users.profile_pic, repost.post_id, repost.user_id
FROM posts
JOIN users ON posts.author_id = users.id
LEFT JOIN repost ON posts.id = repost.post_id
JOIN following ON (posts.author_id = following.followee_id) OR (repost.user_id = following.followee_id)
WHERE " . $user_id . " = following.follower_id
ORDER BY posts.id desc";
I'm not sure how MySQL handles things exactly, but you may still have problems with NULLs from the LEFT JOIN when you do the following table join. I'd run the query in a Query Analyzer to determine if you're getting expected results.

It is because you have an "inner join" on repost ("join" is really short for "inner join"):
JOIN repost ON posts.id = repost.post_id
Doing this means that you will only ever get a record in your result set if there is a matching record in repost
Instead try an "outer join" or "left join"
LEFT JOIN repost ON posts.id = repost.post_id
This should mean that you get records even if there is nothing in repost
I have simplified your tables a little (removed columns), but proven the point with a SqlFiddle here: http://www.sqlfiddle.com/#!2/ee33a

Related

Combine two queries into one effective query

So I have a bit of a problem. Usually when I have two queries I would use JOIN to merge them together, but this time I can't seem to logically figure out how to do it the best since one of the queries also contain a subquery.
Background: I'm setting up a system where people can make posts. The users will then be able to follow other users and topics. So if a post gets tagged with "eduction" - the user will be able to follow the topic "education" - and see all the posts from this topic.
The two queries shows the desired content. But I would like to make a "feed" so the user will see the posts from the others users it follows and from the topics that it follows. I would really like to make it as effecient as possible and I would really like to learn how to handle this in the future myself, so if you could also explain how and why the "solution" would be the best - I would appreciate it.
Query that displays the posts from the users followed:
$query = "SELECT
t.t_id,
t.t_thought,
t.t_timestamp,
t.u_id AS t_u_id,
t.t_statistics,
t.t_reaction,
t.t_comments,
t.t_no_reactions,
t.t_no_comments,
u.u_id AS u_u_id,
u.u_username,
u.u_real_name,
u.u_profile_picture,
r.r_id
FROM thoughts AS t
INNER JOIN users AS u ON t.u_id = u.u_id
INNER JOIN followers AS f ON f.u_id = '".$u_id."'
LEFT OUTER JOIN reactions AS r ON r.u_id = ? AND r.t_id = t.t_id
WHERE t.u_id = f.u_id_to_follow
ORDER BY t_id DESC LIMIT ?, ?
";
Query that displays the posts from the topics followed
$queryGetThoughts = "SELECT
t.t_id,
t.t_thought,
t.t_timestamp,
t.u_id AS t_u_id,
t.t_statistics,
t.t_reaction,
t.t_comments,
t.t_no_reactions,
t.t_no_comments,
u.u_id AS u_u_id,
u.u_username,
u.u_real_name,
u.u_profile_picture,
r.r_id
FROM thoughts AS t
INNER JOIN
users AS u ON t.u_id = u.u_id
INNER JOIN
thoughts_tags AS tt ON tt.t_id = t.t_id
INNER JOIN
tags AS tags ON tt.tag_id = tags.tag_id
LEFT OUTER JOIN reactions AS r ON r.u_id = ? AND r.t_id = t.t_id
WHERE tt.tag_id IN( SELECT tag_id FROM tags_followers WHERE u_id = '".$u_id."')
GROUP BY t.t_id
ORDER BY t.t_id DESC LIMIT ?, ?
";
I appreciate your help and knowledge.
Thank you.

MYSQL JOIN on multiple tables returning no results

Well let's see, the query I have is working fine, as soon as a friendpost is done, however. If the user has no friends, no result will be returned, and that's what I am trying to get a hang of...
$query = "SELECT DISTINCT m.id, p.byuser, p.`newpost`, p.`id`, p.`postdate`
FROM users m
JOIN pinnwand p
ON m.id = p.byuser
JOIN friends f
ON f.`userid` = m.id OR f.`friendid` = m.id
WHERE (f.`userid` = $myId OR f.`friendid`= $myId)
AND (p.`touser` = p.`byuser` OR p.`touser` = $myId)
AND p.`publicp` < 3
AND p.`typ` = 2
ORDER BY p.id DESC LIMIT $limit, 10";
I hope somebody can help me, maybe I am just blind for nao...
Edit
As Steven helped me out quite a lot, maybe somebody finds the last bit missing: It's just showing the posts made for the specific user. Even though as I understand the query it should get the posts made by friends on their pinboard as well? After all the m.id should get the friendtables value as well, or am I wrong?
Edit 2
So as I went with the UNION and Subquery Method for now, I still want to describe what the result should look like:
Show: Userposts made whereever, Posts by whomever made on the Userboard, Friendposts made on their own board! Not the posts made by people on friends boards.
There are 2 problems:
You need a LEFT JOIN on friends. A LEFT JOIN says to return all records from the first table in the join even if there are no results found in the second table in the join.
You also should the WHERE clause conditions relating to friends into the LEFT JOIN clause, so that the conditions occur at the join. You should also be using m.id wherever possible in your joins instead of $myId to eliminate redundancy.
Your WHERE clause is too restrictive (redundant conditions). Always use the simplest set of conditions possible, and put as many as appropriate at the JOIN so they are easier to read.
Example (Edited to add posts from friends, as well):
$query = "SELECT DISTINCT `u`.`id`, `p`.`byuser`, `p`.`newpost`, `p`.`id`, `p`.`postdate`
FROM `users` AS `u`
LEFT JOIN `friends` AS `f`
ON `f`.`userid` = `u`.`id`
OR `f`.`friendid` = `u`.`id`
JOIN `pinnwand` AS `p`
/* This will get all posts made by the user */
ON `p`.`byuser` = `u`.`id`
/* This will get all posts made TO the user by friends */
OR (`p`.`byuser` IN (`f`.`userid`, `f`.`friendid`)
AND `p`.`touser` = `u`.`id`)
WHERE `u`.`id` = {$myId}
AND `p`.`publicp` < 3
AND `p`.`typ` = 2
ORDER BY `p`.`id` DESC
LIMIT {$limit}, 10";
OK, so in the end i ended up using an Union and a subquery... It's probably suboptimal, but if somebody has a good suggestion what to improve, please give me your opinion! Otherwhise I hope that this post will help people with simmilar problems.
$query = "SELECT DISTINCT `p`.`byuser`, `p`.`newpost`, `p`.`id`, `p`.`postdate`
FROM `pinnwand` AS `p`
JOIN `users` AS `u` ON `u`.`id` = `p`.`byuser`
LEFT JOIN `friends` AS `f` ON (`f`.`friendid` = `u`.`id` OR `f`.`userid` = `u`.`id`)
WHERE (f.userid = {$myId} OR f.friendid = {$myId})
AND `p`.`publicp` < 3
AND `p`.`typ` = 2
AND `p`.`byuser` <> {$myId}
UNION ALL
SELECT DISTINCT `p`.`byuser`, `p`.`newpost`, `p`.`id`, `p`.`postdate`
FROM `pinwand` AS `p`
JOIN `users` AS `u` ON `u`.id = `p`.`byuser`
WHERE `u`.`id` = {$myId}
AND `p`.`publicp` < 3
AND `p`.`typ` = 2
ORDER BY `postdate` DESC
LIMIT 0,10";

mysql left join with 9 tables

So the situation is I have a query that involves 9 tables and I need to write it so it returns all records even when the impactid in the workorderstates table is NULL.
Previous to the below query I noticed I wasn't getting all results that were "open" because initially I just had where workorderstates.impactid = impactdefiniton.impactid and in the situations where impactid is NULL in the workorderstates table this condition would not be true, thus eliminating records that should be returned because they are in fact "open".
So I devised this query below but every time I run it it will not work. It will will return not unique table alias workorder. If I use aliases for tables it just moves on the right tables in the join as not being unique. Can anyone offer me any help on restructuring the query so it will work? I've tried a lot of variations and interestingly enough the second query ALMOST works but it returns duplicate records (in this case four of the same record)
select workorder.workorderid, workorder.siteid,
FROM_UNIXTIME(workorder.CREATEDTIME/1000, '%m-%d-%Y %H:%i:%s') as createdate,
categoryname, IFNULL(workorderstates.impactid, "No Set") as impactid,
IFNULL(impactdefinition.name, "Not Set") as impactname, first_name,
sdorganization.name, statusname, title
from workorder, statusdefinition, sitedefinition, sdorganization,
prioritydefinition, categorydefinition, sduser, aaauser, workorderstates
left Join impactdefinition on workorderstates.impactid = impactdefinition.impactid
left join workorder on workorder.workorderid = workorderstates.workorderid
left join workorderstates on workorderstates.statusid = statusdefinition.statusid
left join workorder on workorder.siteid = sitedefinition.siteid
left join sitedefinition on sitedefinition.siteid = sdorganization.org_id
left join workorderstates on workorderstates.categoryid = categorydefinition.categoryid
left join workorder on workorder.requesterid = sduser.userid
left join sduser on sduser.userid = aaauser.user_id
where statusname='Open' and workorder.createdtime >= '1352678400000'
and sdorganization.name='MAPL'
order by workorder.workorderid
Query that almost works but is ugly (returns duplicated records):
select workorder.workorderid, workorder.siteid,
FROM_UNIXTIME(workorder.CREATEDTIME/1000, '%m-%d-%Y %H:%i:%s') as createdate,
categoryname, IFNULL(workorderstates.impactid, "No Set") as impactid,
IFNULL(impactdefinition.name, "Not Set") as impactname, first_name,
sdorganization.name, statusname, title
from workorder, statusdefinition, sitedefinition, sdorganization,
prioritydefinition, categorydefinition, sduser, aaauser, workorderstates
left Join impactdefinition on workorderstates.impactid = impactdefinition.impactid
where workorder.workorderid = workorderstates.workorderid
and workorderstates.statusid = statusdefinition.statusid
and workorder.siteid = sitedefinition.siteid
and sitedefinition.siteid = sdorganization.org_id
and workorderstates.categoryid = categorydefinition.categoryid
and workorder.requesterid = sduser.userid and sduser.userid = aaauser.user_id
and statusname='Open' and workorder.createdtime >= '1352678400000'
and sdorganization.name='MAPL'
order by workorder.workorderid
Any ideas of how to get this query working??? Thanks guys!
I took a look at your query and I think you have some basic misunderstandings about JOINs and how to write them. It's like you're just guessing at syntax at random, and that's not the way to write code.
I examined your query and converted it into SQL-92 syntax. I had to make some inferences about join conditions, so I can't guarantee it's correct for your application, but it's a lot closer to a legal query.
Only I couldn't find any condition in your example for the join to your prioritydefinition table. That's likely to be the cause of your duplicate rows. You're generating what's called a Cartesian product.
select workorder.workorderid, workorder.siteid,
FROM_UNIXTIME(workorder.CREATEDTIME/1000, '%m-%d-%Y %H:%i:%s') as createdate,
categoryname, IFNULL(workorderstates.impactid, "No Set") as impactid,
IFNULL(impactdefinition.name, "Not Set") as impactname, first_name,
sdorganization.name, statusname, title
from workorder
inner join statusdefinition on workorderstates.statusid = statusdefinition.statusid
inner join sitedefinition on workorder.siteid = sitedefinition.siteid
inner join sdorganization on sitedefinition.siteid = sdorganization.org_id
inner join prioritydefinition ...NO JOIN CONDITION FOUND...
inner join categorydefinition on workorderstates.categoryid = categorydefinition.categoryid
inner join sduser on workorder.requesterid = sduser.userid
inner join aaauser on sduser.userid = aaauser.user_id
inner join workorderstates on workorder.workorderid = workorderstates.workorderid
left Join impactdefinition on workorderstates.impactid = impactdefinition.impactid
where statusname='Open'
and workorder.createdtime >= '1352678400000'
and sdorganization.name='MAPL'
order by workorder.workorderid
You really need to get someone who knows your application and also knows how to write SQL to tutor you before you write any more SQL joins.
I too reformatted your query but have it more visually hierarchical to show relations from the first (left-side) table to what it is getting its details from (right-side) table. As Bill mentioned, you had an extra table that was not doing anything and thus your Cartesian product.
Now, if you ARE stuck and have no one else to really help, here is a basic of LEFT-JOIN vs INNER-JOIN. Left-join basically says I want every record from the table on the left (as I have listed first) REGARDLESS of there being a record found on the right side. If there IS a match, great, no problem... but if no match, your query will still run.
So, I've set to all LEFT-JOINs. You can change as needed for those you know MUST always exist... such as a work order is entered by a "user". So that you could change. Hopefully this helps you out. Also look at how I've nested from work order to work order states and from work order states to get the corresponding status definition and other things associated with the work order states table. Others were directly related with the work order, so those are at THAT hierarchical level.
One last note... not all your fields were table.field reference (which I changed to aliases to help readability). QUALIFY ALL your fields so you and others trying to help, or read your code in the future know the origin of the field, not just guessing (its in one of the tables)
select
WO.workorderid,
WO.siteid,
FROM_UNIXTIME(WO.CREATEDTIME/1000, '%m-%d-%Y %H:%i:%s') as createdate,
categoryname,
IFNULL(WOS.impactid, "No Set") as impactid,
IFNULL(ImpD.name, "Not Set") as impactname, first_name,
SDO.name,
statusname,
title
from
workorder WO
LEFT JOIN workorderstates WOS
ON WO.workorderid = WOS.workorderid
LEFT JOIN statusdefinition StatD
ON WOS.statusid = StatD.statusid
LEFT JOIN categorydefinition CatD
ON WOS.categoryid = CatD.categoryid
LEFT JOIN impactdefinition ImpD
ON WOS.impactid = ImpD.impactid
LEFT JOIN sitedefinition SiteD
ON WO.siteid = SiteD.siteid
LEFT JOIN sdorganization SDO
ON SiteD.siteid = SDO.org_id
and SDO.name = 'MAPL'
LEFT JOIN sduser U
ON WO.requesterid = U.userid
LEFT JOIN aaauser AU
ON U.userid = AU.user_id
where
statusname = 'Open'
and WO.createdtime >= '1352678400000'
order by
WO.workorderid

get count of posts based on count(*)

i am trying to get number of posts that i have
Here is my query
$Query="
SELECT t.*,u.*,c.*
FROM posts as t
LEFT JOIN relations as r on r.post_id = t.post_id
LEFT JOIN users as u on t.auther_id = u.auther_id
LEFT JOIN categories as c on c.cate_id = r.cate_id
GROUP BY t.post_id
";
$Query=mysql_query($Query);
$numberOfPosts=mysql_num_rows($Query);
This query is works very well
but i am trying to convert it, i want make it faster
i want use count(*) instead of t.*
because when i use t.*, it gets the full data of posts and categories
but i want to get count only, So i decided to use count(*) but i don't know how to use it with query like this
Edit
i've replaced SELECT t.*,u.*,c.* with SELECT count(t.*)
But i got mysql Error Warning: mysql_fetch_assoc(): supplied argument
Edit 2:
i am trying SELECT count(t.post_title)
I Got this results
Array ( [count(t.post_id)] => 10 )
But i have only 2 posts!
$Query="
SELECT t.*,u.*,c.*
FROM posts as t
LEFT JOIN relations as r on r.post_id = t.post_id
LEFT JOIN users as u on t.auther_id = u.auther_id
LEFT JOIN categories as c on c.cate_id = r.cate_id
GROUP BY t.post_id
";
$Query=mysql_query($Query);
$numberOfPosts=mysql_num_rows($Query);
Let's take a step back and analyze this query for a moment.
You're selecting everything from three out of four tables used in the query. The joins create some logic to limit what you select to the proper categories, authors, etc. At the end of the day you are getting a lot of data from the database, then in PHP simply asking it how many rows were returned (mysql_num_rows). Instead, what #Dagon is trying to suggest in comments, is that you have MySQL simply count the results, and return that.
Let's refactor your query:
$query = "
SELECT COUNT(t.post_id) AS qty
FROM posts as t
LEFT JOIN relations AS r ON r.post_id = t.post_id
LEFT JOIN users AS u ON t.auther_id = u.auther_id
LEFT JOIN categories AS c ON c.cate_id = r.cate_id
GROUP BY t.post_id
";
$result = mysql_query($query);
$result_row = mysql_fetch_assoc($result);
$numberOfPosts = $result_row['qty'];
(You could also use Barattlo's custom execute_scalar function to make it more readable.)
I would need to see your table structures to be of more help on how to optimize the query and get the desired results.
try doing this:
$Query="
SELECT count(t.*) as count_all
FROM posts as t
LEFT JOIN relations as r on r.post_id = t.post_id
LEFT JOIN users as u on t.auther_id = u.auther_id
LEFT JOIN categories as c on c.cate_id = r.cate_id
GROUP BY t.post_id
";
$Query=mysql_query($Query);
$numberOfPosts=mysql_num_rows($Query);
You want to do
SELECT count(t.id) AS count FROM ....
//do query with PDO or whatever you are using
$rows = mysql_fetch_assoc();
$num_rows = $rows['count'];
You should probably simply use
SELECT count(*) as postingcount FROM posts
Why?
Because you do not have a WHERE clause, so there are no restrictions. Your JOINS do not ADD more rows to the resultset, and in the end your GROUP BY merges every duplicate occurance of a post_id that might have occurred because of joining back into one row. The result should only be counted, so assuming that the real number you want to know is the number of data sets inside the table posts, you do not need any join, and doing count(*) really is a very fast operation on tables in MySQL.
Remember to check if mysql_query returns false, because then you have to check mysql_error() and see why your query has an error.

expanding a mysql JOIN query in PHP

I need to alter my existing JOIN query below to also include the data from users.image correlating to the UserID of the post maker. Something like:
users.image WHERE users.UserID = posts.userid
I am not very good with join queries yet. How would I do this?
Existing Query:
$result = mysql_query("SELECT posts.* FROM listen JOIN posts ON posts.userid = listen.listenid WHERE listen.userid = '$user_id' ORDER BY DATE desc") or die(mysql_error());
Just add another JOIN clause:
SELECT posts.*
FROM listen
JOIN posts ON (posts.userid = listen.listenid)
JOIN users ON (users.UserID = posts.userid)
WHERE listen.userid = '$user_id'
ORDER BY DATE desc
You may need to change the JOIN to a specific join such as LEFT JOIN, depending on what you're after.
Btw, it is easier to see the query on multiple lines.
Edit: You'll probably want to add additional items that you are selecting with your fields, such as SELECT posts.*, users.*

Categories