I have a table that contains the information of all the posts, table's name is "paylasimlar". and I have another table that contains the information of every like action, table's name is "begeniler". It has 2 columns:
1-User ID of who liked the post
2-The Post ID that liked
The thing I want to do is to write a query with joins that returns all the information of the posts from table "paylasimlar" with the count of likes it got. The problem I ran into is; if a post hasn't got liked yet there would be no information on the "begeniler" table and it would not return the information of that table as a row. Can someone help?
select a.*,count(b.PostID) from paylasimlar a
left join begeniler b on a.PostID and b.PostID group by b.PostID
Related
How does Twitter (or Facebook) do to retrieve a user feed?
Here is the database schema I am working on. I renamed everything to look like twitter hoping that the most of you would understand my question...
I only have two resources: users and tweets. So here are those two tables:
users:
- id
- username
tweets:
- id
- author_id
- content
Let's continue with a simple pivot table to associate tweets and users:
user_tweet:
- user_id
- tweet_id
- created_at
This pivot table is here mainly to store the retweets. But it also stores the original tweets for more convenience.
For example, let's take user 1 tweets 'something'. user 2 and user 3 retweet. The user_tweet table will have three rows.
And, let's see a last table that complicates everything: The Following System.
Every user can follow an other user. I named the table "followee_follower":
followee_follower:
- followee_id
- follower_id
The followee_id is the user.id of the person being followed
The follower_id is the user.id of the person that follows an other one
Now let's get to the SQL problem:
I'm user 1. I follow user 2, and user 3.
How can I retrieve the tweets and retweets from user 2 and user3 knowing that I want to retrieve them ordering them by the created_at field of the user_tweet table, and that I don't want to get two similar tweets.
Many thanks, any help is highly nice from you,
Have a good day/night.
EDIT: Here are some samples data from tables:
users table
tweets table
user_tweet table
followee_follower table
expected results
I'm not sure I completely understand your question (sample data would help), but I think you just need to use multiple joins:
select t.id, t.content, ut.created_at
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
order by ut.created_at
Perhaps if a user retweets, you'd want to do something like this instead to get the first tweet (assuming the id and created_at fields both should return the minimum):
select t.content, min(t.id), min(ut.created_at)
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
group by t.content
order by min(ut.created_at)
I need to select all the messages (first posts and replies) posted in an array of specific categories (forums) in Drupal.
First posts are stored in field_data_body, replies are stored in field_data_comment_body.
The structure of field_data_body and field_data_comment_body is the same, in the column body_value there's the content of the posts and in the column entity_id their unique ID.
The table field_data_taxonomy_forums contains the entity_id column and the taxonomy_forums_tid column (which are the IDs of the forum categories). The table taxonomy_term_data contains the columns tid (which is the same of taxonomy_forums_tid and the description column (which is the title of the forum category).
So, I'm looking for a query that allows me to select the body of the posts (both first posts and replies) and the description of the forum specifying an array of tids (i.e. the IDs of the forum categories), that I'll manually find in the taxonomy_term_data table.
So, for example I'm looking for the query that allows me to SELECT the posts "belonging" to tids 1456,7622,862 and the relative tid description.
Here's a screenshot of the field_data_body table :
I think, it would be better for us to split the task into 2 subtasks:
Find the bodies of the posts, belonging to a particular tid.
Find all the comments of the body, belonging to a particular tid.
We'll need to use these tables:
field_data_taxonomy_forums
field_data_body
comment
field_data_comment_body
Database structure:
Finding the bodies
SELECT
taxonomy_forums.taxonomy_forums_tid AS tid,
body.entity_id,
body.body_value AS body
FROM
field_data_taxonomy_forums AS taxonomy_forums
INNER JOIN
field_data_body AS body
ON
body.entity_id=taxonomy_forums.entity_id
WHERE
taxonomy_forums.taxonomy_forums_tid IN (9);
Finding the comments for the bodies
Here we'll need comments table, that unites field_data_body and field_data_comment_body.
SELECT
taxonomy_forums.taxonomy_forums_tid AS tid,
comment_body.entity_id,
comment_body.comment_body_value AS body
FROM
field_data_taxonomy_forums AS taxonomy_forums
INNER JOIN
field_data_body AS body
ON
body.entity_id=taxonomy_forums.entity_id
INNER JOIN
comment
ON
comment.nid=body.entity_id
INNER JOIN
field_data_comment_body AS comment_body
ON
comment_body.entity_id=comment.cid
WHERE
taxonomy_forums.taxonomy_forums_tid IN (9);
If you UNION these 2 queries, you'll get the list of posts and comments.
sqlfiddle
To find reference to forum id you must add use of "forum" table (table establishing relationship of nodes to forum terms.)
Also it should be noted that forum id does not exist, it is a taxonomy id.
Here is an example to get only first message topic by taxonomy id(tid)
SELECT * FROM field_data_body fdb
LEFT JOIN forum f ON f.nid = fdb.entity_id
WHERE fdb.bundle="forum" AND f.tid=15
Note that the forum module works with comment core module wich means that only first topic message is stored in "field_data_body" and all replies are stored in "field_data_comment_body" table.
Hope it helps
I know all about joining tables, but I'm having trouble figuring out what to do in this case.
I have a table users which contains a column called user_id which is an integer.
I have a second table called "follows" which contains a column "user_id" and a column called "follow_id". This table is used to log which other users each user follows (think Twitter).
I have a third table called posts which contains a column called user_id , which is a foreign key.
Ideally I'd like a single query that looks up the follows table, gets all the follow_ids from for a single user, then returns all posts from the posts table.
I suggest you to use LEFT JOIN
SELECT
follows.follow_id,
posts.*
FROM
follows
LEFT JOIN
post ON posts.user_id = follows.user_id
WHERE
follows.user_id = <UserID>
Sir,
The concept of my project is ,the logged user can upload three different photos and the deatils are stored in b_photodetails table and the fields are(id,photoid,student_id,userid,count,likes,subdate ) idas1,2,3...ect photoid means the name of the store photo file userid and count is the the number of photoes uploaded ie 1,2 and 3anly.and likes is the total number of likes for each photos.And i Have another table like_master containing the like details and the fields are(like_id,userid, liked_date) ,the like id in this table is same as id in the b_photodetails table ,and the liked_date is the date in which the photo is liked.So i need to get the number of likes between two particular
dates.Please help me to frame a query to solve this in MySQL.
For eg:If we click a like for where id=1,the likes in b_photodetails will be 1 and the like_master table will be(like_id,userid, liked_date) (1,userid eg:sree,2014-01-28)
I don't really know for sure what you are asking, but, here is my stab in the dark:
select sum(likes) from like_master where liked_date between 'date1' and 'date2';
or possibly
select sum(lm.likes)
from b_photodetails as b
right join like_master as lm
on b.userid = lm.userid
where lm.liked_date between 'date1' and 'date2';
I don't understand much what you are asking but may be it is like this
Select b.*,(SELECT COUNT(like_id) from like WHERE b.id = like.photo_id) as total_likes FROM b.photodetails
I have four tables
post
-------------
post_id
cat_id
posts
post_category
-------------
cat_id
cat_name
users
-------------
user_id
user_name
user_category_map
-------------
user_id
cat_id
I want all posts added by all users in all post categories
I have written this query
SELECT posts
FROM post p, users u, user_category_map ucm
WHERE p.cat_id = ucm.cat_id
AND ucm.user_id = u.user_id
but I am getting repeated posts. Is my table structure correct and normalized properly. I am not able to correctly grab the logic. Is the join I put is correct?
Here you received the full cartesian product of all tables, and filter it by " p.cat_id = ucm. cat_id and ucm.user_id = u.user_id" condition.
Query like this typically converted to optimized join versions - merge, nested loop, index or hash join.
During cartesian product A*B*C every row from A will be repeat B*C times.
Your query "all posts added by all users in all post categories" is
select * from post
The design does not look correct. Instead of a reference table for user_category, you should have a reference table for user_post. In current scenario, if same user adds multiple posts for same category, you will end up having duplicate rows in user_category_map