I'm having trouble with my most confusing query. It worked before but I must have changed a variable a couple of months ago, so now it doesn't work.
What it is meant to do is:
select * from users, * from media and * from friends
then allow the media to only be displayed if it contains a friends ID or you're own ID. However it its displaying the friends media but not my own. And the friends system is as follows: id, senders_id, recipient_id, status
Status being 0 (not accepted) and 1 (accepted)
Here is the following query:
SELECT DISTINCT users.id, users.firstname, users.lastname,
media.date, media.id, media.time, media.text, media.userID,
media.author_id, media.ip, media.post_id, media.format,
media.file_format, media.MediaTxt, media.author_firstname,
media.author_lastname, media.shared, media.relation
FROM users
JOIN friends
ON users.id IN (friends.sender, friends.recipient)
JOIN media
ON (users.id = media.userID)
WHERE 151 IN (friends.sender,friends.recipient)
AND media.relation = 'feed'
ORDER BY media.time DESC, media.date DESC
Primary and foreign keys:
friends.id (PRI Key)
media.id (PRI Key)
users.id (PRI Key)`
--
users.id is the same as media.UsrID and friends.recipient/sender
I think the big problem is that if a person has no friends (eg, user.id 151 above I'm guessing?) then the JOIN friends will result in zero rows returned.
You probably want to use LEFT JOINs, however there's bigger problems than that - the whole query is kind of hard to understand and perhaps not the best way to go about what you're after.
You're kind of saying "list each user and their friends along with all media they've uploaded. If user id 151 appears as either the sender or recipient in the friend relationship, give me the media." Even if you use left joins, that's not going to help friendless people, because those columns will be null. You need to also add in the so.users_id to the compare list, eg:
SELECT DISTINCT users.id, users.firstname, users.lastname,
media.date, media.id, media.time, media.text, media.userID,
media.author_id, media.ip, media.post_id, media.format,
media.file_format, media.MediaTxt, media.author_firstname,
media.author_lastname, media.shared, media.relation
FROM users
LEFT JOIN friends
ON users.id IN (friends.sender, friends.recipient)
JOIN media
ON (users.id = media.userID)
WHERE 151 IN (users.id, friends.sender,friends.recipient)
AND media.relation = 'feed'
ORDER BY media.time DESC, media.date DESC
Note, I didn't bother LEFT JOINing media, since it's media you're after and returning blanks is kind of pointless.
The query is still written a little backwards though - it might be clearer if you wrote it something like:
SELECT DISTINCT users.id, users.firstname, users.lastname,
media.date, media.id, media.time, media.text, media.userID,
media.author_id, media.ip, media.post_id, media.format,
media.file_format, media.MediaTxt, media.author_firstname,
media.author_lastname, media.shared, media.relation
FROM users
JOIN media
ON (users.id = media.userID)
LEFT JOIN friends
ON users.id IN (friends.sender, friends.recipient)
WHERE 151 IN (users.id, friends.sender, friends.recipient)
AND media.relation = 'feed'
ORDER BY media.time DESC, media.date DESC
Also, I sure hope 151 is replaced with a positional parameter, so you don't introduce SQL injection attacks!
Related
I have 3 tables, users, news, news_viewed. I'm trying to join these 3 tables and find a list of news each user has not viewed.
TABLE users
userid
username
status
TABLE news
newsid
title
post_time
TABLE news_viewed
nvid
username
newsid
Looking to find a list from users that have not read news (found in news_viewed)
I've tried many different joins, including left joins and inners and outers but cannot get the results I need.
$_30daysago = strtotime('-30 days');
SELECT * FROM
(
SELECT users.username, news_id
FROM users inner join news_viewed ON
users.username = news_viewed.username and users.status='active'
UNION
SELECT news_viewed.username, post_time
FROM news_viewed inner join news ON
news_viewed.newsid = news.newsid and news.post_time>'$_30daysago'
) as JoinedTable
I need the required results to include the users.username, news.newsid and news.title.
Any help would be appreciated, thank you!
This is a good spot to use the LEFT JOIN antipattern:
SELECT u.username, n.newsid, n.title
FROM users u
INNER JOIN news n ON n.post_time > ?
LEFT JOIN news_viewed nv
ON n.newsid = nv.newsid
AND nv.username = u.username
WHERE
u.status = 'active'
AND nv.nvid IS NULL
This query generates a cartesian product of users and recent news (ie having a post time greater than the parameter indicated by ?), and returns the users/news tuples for which the left join on news_viewed did not succeed (hence the antipattern).
Note: it is unclear what column to use in the join; column name news_viewed (username) tend to indicate that it relates to users(username), whereas the primary key of users seems to be userid. Fix your column names or fix your relationship.
Ellaborating on #GMB's answer
Your query:
$_30daysago = strtotime('-30 days');
SELECT * FROM
(
SELECT users.username, news_id
FROM users inner join news_viewed ON
users.username = news_viewed.username and users.status='active'
UNION
SELECT news_viewed.username, post_time
FROM news_viewed inner join news ON
news_viewed.newsid = news.newsid and news.post_time>'$_30daysago'
) as JoinedTable
is saying:
get all active users with the news they have read (inner join)
SELECT users.username, news_id
FROM users inner join news_viewed ON
users.username = news_viewed.username and users.status='active'
and add all the news with the users that have read them in the last 30 days (inner join again)
SELECT news_viewed.username, post_time
FROM news_viewed inner join news ON
news_viewed.newsid = news.newsid and news.post_time>'$_30daysago'
That is actually bringing up all the tuples from news_viewed minus the ones where the user is not active AND the new is over 30 days old.
however, given the usage of inner join, you're bringing a lot of duplicate records
1.- The results from the first query where the new is less than 30 days old
2.- The results from the second query where the user is active
since you're using UNION and not UNION ALL, you are implicitly asking for a SELECT DISTINCT, but the fields are different (it makes no sense to display newsid and then post_time in the same field)
plus, you have a typo in the field name, which is not news_id
You have to look at it from the other way around. The potential combinations amount for a scenario where every user has read every new. So you get that universe as a basis (number of users times number of news) and then
1- remove inactive users
2- remove news older than 30 days
3- remove tuples that are unrelated in the news_viewed table
SELECT users.username, news.newsid
FROM users
JOIN news
ON users.status='active' -- removes inactive users
AND news.post_time>'$_30daysago' -- removes older news
LEFT JOIN
news_viewed nv USING (username, newsid)
WHERE nv.nvid IS NULL -- removes unrelated entries
I don't think this will be too complicated to explain, but certainly complicated to get it working.
First of all, I have a couple of tables regarding users comments, one table for each section (forum, articles etc), as shown below:
site_users (id, username, ...) [Table that holds user's info]
site_articles_comments (id, user_id, comment, ...) [Where user_id = site_users.id]
site_forum_comments (id, user_id, comment, ...) [Same for site_articles_comments]
The thing is that every new row is a new comment and users can comment multiple times, which means that more rows are being added, thus making the need of sorting the number of rows to get the amount of comments in some sort of ranking system.
I was able to make a simple forum rank by doing this simple query:
SELECT u.id, u.username, COUNT(r.id) AS rank FROM site_users AS u LEFT
JOIN site_forum_comments AS r ON u.id = r.user_id GROUP BY u.username,
u.id ORDER BY rank DESC LIMIT :l
This query sorts all users from the database, where the user who has commented the most is always on top.
What I need, in the other hand, is to have a global ranking system, which sums the amount of comments in each section (articles, forum etc) and displays the users accordingly.
I was playing around with the sql to do that and the last thing I came up with was this huge query:
SELECT u.id, u.username, (COUNT(a.id) + COUNT(f.id)) AS rank FROM
site_users u LEFT JOIN site_articles_comments a ON a.user_id = u.id
LEFT JOIN site_forum_comments f ON f.user_id = u.id GROUP BY
u.username, u.id ORDER BY rank DESC LIMIT :l
This, however, returns null. What could I possibly do to achieve the result I want?
Thanks in advance,
Mateus
EDIT1: Sorry for the lack of information, this is regarding MySQL.
The problem is math with nulls, and ordering with nulls (check into the "NULLS LAST" option for overriding the default ordering which returns the nulls first for a descending order).
In your case, with the outer joins, if the user has a ton of article comments but no forum comments, well, 100 + null = null in Oracle math. So to get the math to work you need to make null=0. That's where NVL() comes in (and also has the nice side-effect of eliminating pesky nulls from your result set)!
SELECT u.id, u.username, (NVL(COUNT(a.id),0) + NVL(COUNT(f.id),0)) AS rank
FROM site_users u
LEFT JOIN site_articles_comments a ON a.user_id = u.id
LEFT JOIN site_forum_comments f ON f.user_id = u.id
GROUP BY u.username, u.id ORDER BY rank DESC LIMIT :l
I see you have both MySQL and Oracle in your tags - the above is for Oracle. If for MYSQL use COALESCE(COUNT(),0) instead.
try SELECT u.id, MIN(u.username) AS username, (COALESCE(COUNT(DISTINCT(a.id)),0) + COALESCE(COUNT(DISTINCT(f.id)),0)) AS rank
FROM site_users AS u
LEFT JOIN site_articles_comments AS a ON (a.user_id = u.id)
LEFT JOIN site_forum_comments AS f ON (f.user_id = u.id)
GROUP BY u.id
ORDER BY rank DESC
LIMIT :l
I am trying to work out the most efficient query to put data from two tables into one set of results and left join the user data to it. I need help with the syntax for the query. I have put together the below but it's not quite right.
$query_chat = sprintf("SELECT comment_id, user_id, comment, timestamp FROM activity
UNION ALL
SELECT comment_id, user_id, comment, timestamp FROM comments
LEFT JOIN users.company, users.contact_person, users.email ON users.user_id = comments.user_id
WHERE user_id = %s
ORDER BY timestamp DESC", GetSQLValueString($_GET['comment_id'], "INT"));
The syntax is wrong and throws up the below - I am trying to make it efficient by only selecting what I need from the users table.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' users.contact_person, users.email ON users.user_id = comments.user_id WHERE us' at line 4
I have now got to this point below but get Unknown column 'users.company' in 'field list'
SELECT
comments.comment_id, comments.user_id, comments.comment,
comments.timestamp,
users.company, users.contact_person, users.email
FROM
comments
UNION ALL
SELECT
activity.comment_id, activity.user_id, activity.comment,
activity.timestamp,
users.company, users.contact_person, users.email
FROM
activity
LEFT JOIN users ON users.user_id = comments.user_id
WHERE
comments.comment_id = 69 ORDER BY timestamp ASC
Here is your problem:
LEFT JOIN
users.company, users.contact_person, users.email
ON users.user_id = comments.user_id
The syntax is:
LEFT JOIN (table) ON (join-clause)
So, in your case, I think you need something like this:
SELECT
comments.comment_id, comments.user_id, comments.comment,
comments.timestamp,
users.company, users.contact_person, users.email
FROM
comments
LEFT JOIN users ON users.user_id = comments.user_id
WHERE
users.user_id = %s
;
As you can see, when using a JOIN, the columns come first (from all tables) and then the tables come next, with their corresponding JOIN clauses.
Update: in reply to your update, you have two options. You can do this (which you are closest to):
SELECT
(users joined to activity)
UNION
(users joined to comments)
You already know how to do the (expressions) above - see the earlier part of my answer. Or, you can do things this way:
SELECT
*
FROM
(SELECT * FROM activity UNION SELECT * FROM comments) user_actions
LEFT JOIN users ON (users.user_id = user_actions.user_id)
Note that in both examples cases in this update, I am offering pseudocode1 - you need to fill in the blanks. You'll find that the skill of taking a generalised example (say from a manual) and applying to your own use case is something that happens a great deal in computer science, so you should practice this as often as possible.
1 That said, the second version containing the sub-query is done except for changing the columns to fetch - so you are probably 95% of the way there!
So finally got there with the below, appreciate the direction halfer
SELECT
comments.*, users.company, users.contact_person, users.email
FROM
comments
LEFT JOIN users ON users.user_id = comments.user_id
WHERE
comment_id = %s
UNION ALL
SELECT activity.*, users.company, users.contact_person, users.email
FROM
activity
LEFT JOIN users ON users.user_id = activity.user_id
WHERE
comment_id = %s
ORDER BY
timestamp ASC
I'm having issues writing this query, it's an odd relation that I can't figure out. I'm wondering if it would be better to just use two mysql queries and merge the results with php?... Anyways.. so here we go.
Here's the tables we're using:
- media -
id
userId
accessKey
internalName
type
created
modified
- reposts -
id
userId
mediaId
created
- users -
id
username
Basically, what I want to do is get a result set of media items associated with the user who posted it, and then ALSO, in the same result set, include additional rows for media items that have been reposted, and then for reposted media items, instead of associating the media.userId of the media item for the username association, associate the reposts.userId as the username.
Here's a rough idea to illustrate, these two example queries below need to work as 1 to provide a combined result set.
SELECT media.*, users.username,
0 AS reposted
FROM media
LEFT JOIN users ON users.id = media.userId
SELECT media.id, media.accessKey, media.internalName, media.type, media.modified, users.username, reposts.userId, reposts.created,
1 AS reposted
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId
How would I go about doing this? Or would I be better off using 2 queries and merging the results with PHP?
You can use UNION in your query, but UNION requires the same number of columns (and same data type if I recall correctly) on both queries:
(SELECT media.id, media.accessKey, media.internalName, media.type, media.modified, users.username, users.id, media.created,
0 AS reposted
FROM media
LEFT JOIN users ON users.id = media.userId)
UNION
(SELECT media.id, media.accessKey, media.internalName, media.type, media.modified, users.username, reposts.userId, reposts.created,
1 AS reposted
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId)
I'm working on building a forum with kohana. I know there is already good, free, forum software out there, but it's for a family site, so I thought I'd use it as a learning experience. I'm also not using the ORM that is built into Kohana, as I would like to learn more about SQL in the process of building the forum.
For my forum I have 4 main tables:
USERS
TOPICS
POSTS
COMMENTS
TOPICS table: id (auto incremented), topic row.
USERS table: username, email, first and last name and a few other non related rows
POSTS table: id (auto incremented), post-title, post-body, topic-id, user-id, post-date, updated-date, updated-by(which will contain the user-id of the person who made the most recent comment)
COMMENTS table: id (auto incremented), post-id, user-id and comment
On the main forum page I would like to have:
a list of all of the topics
the number of posts for each topic
the last updated post, and who updated it
the most recently updated topic to be on top, most likely an "ORDER BY updated-date"
Here is the query I have so far:
SELECT topics.id AS topic-id,
topics.topic,
post-user.id AS user-id,
CONCAT_WS(' ', post-user.first-name, post-user.last-name) AS name,
recent-post.id AS post-id,
post-num.post-total,
recent-post.title AS post-title,
recent-post.update_date AS updated-date,
recent-post.updated-by AS updated-by
FROM topics
JOIN (SELECT posts.topic-id,
COUNT(*) AS post-total
FROM POSTS
WHERE posts.topic-id = topic-id
GROUP BY posts.topic-id) AS post-num ON topics.id = post-num.topic-id
JOIN (SELECT posts.*
FROM posts
ORDER BY posts.update-date DESC) AS recent-post ON topics.id = recent-post.topic-id
JOIN (SELECT users.*,
posts.user-id
FROM users, posts
WHERE posts.user-id = users.id) as post-user ON recent-post.user_id = post-user.id
GROUP BY topics.id
This query almost works as it will get all of information for topics that have posts. But it doesn't return the topics that don't have any posts.
I'm sure that the query is inefficient and wrong since it makes two sub-selects to the posts table, but it was the only way I could get to the point I'm at.
Dash is not a valid character in SQL identifiers, but you can use "_" instead.
You don't necessarily have to get everything from a single SQL query. In fact, trying to do so makes it harder to code, and also sometimes makes it harder for the SQL optimizer to execute.
It makes no sense to use ORDER BY in a subquery.
Name your primary key columns topic_id, user_id, and so on (instead of "id" in every table), and you won't have to alias them in the select-list.
Here's how I would solve this:
First get the most recent post per topic, with associated user information:
SELECT t.topic_id, t.topic,
u.user_id, CONCAT_WS(' ', u.first_name, u.last_name) AS full_name,
p.post_id, p.title, p.update_date, p.updated_by
FROM topics t
INNER JOIN
(posts p INNER JOIN users u ON (p.updated_by = u.user_id))
ON (t.topic_id = p.topic_id)
LEFT OUTER JOIN posts p2
ON (p.topic_id = p2.topic_id AND p.update_date < p2.update_date)
WHERE p2.post_id IS NULL;
Then get the counts of posts per topic in a separate, simpler query.
SELECT t.topic_id, COUNT(*) AS post_total
FROM topics t LEFT OUTER JOIN posts p USING (topic_id)
GROUP BY t.topic_id;
Merge the two data sets in your application.
to ensure you get results for topics without posts, you'll need to use LEFT JOIN instead of JOIN for the first join between topics and the next table. LEFT JOIN means "always return a result set row for every row in the left table, even if there's no match with the right table."
Gotta go now, but I'll try to look at the efficiency issues later.
This is a very complicated query. You should note that JOIN statements will limit your topics to those that have posts. If a topic does not have a post, a JOIN statement will filter it out.
Try the following query.
SELECT *
FROM
(
SELECT T.Topic,
COUNT(AllTopicPosts.ID) NumberOfPosts,
MAX(IFNULL(MostRecentPost.Post-Title, '') MostRecentPostTitle,
MAX(IFNULL(MostRecentPostUser.UserName, '') MostRecentPostUser
MAX(IFNULL(MostRecentPost.Updated_Date, '') MostRecentPostDate
FROM TOPICS
LEFT JOIN POSTS AllTopicPosts ON AllTopicPosts.Topic_Id = TOPICS.ID
LEFT JOIN
(
SELECT *
FROM Posts P
WHERE P.Topic_id = TOPICS.id
ORDER BY P.Updated_Date DESC
LIMIT 1
) MostRecentPost ON MostRecentPost.Topic_Id = TOPICS.ID
LEFT JOIN USERS MostRecentPostUser ON MostRecentPostUser.ID = MostRecentPost.User_Id
GROUP BY T.Topic
)
ORDER BY MostRecentPostDate DESC
I'd use a left join inside a subquery to pull back the correct topic, and then you can do a little legwork outside of that to get some of the user info.
select
s.topic_id,
s.topic,
u.user_id as last_updated_by_id,
u.user_name as last_updated_by,
s.last_post,
s.post_count
from
(
select
t.id as topic_id,
t.topic,
t.user_id as orig_poster,
max(coalesce(p.post_date, t.post_date)) as last_post,
count(*) as post_count --would be p.post_id if you don't want to count the topic
from
topics t
left join posts p on
t.id = p.topic_id
group by
t.topic_id,
t.topic,
t.user_id
) s
left join posts p on
s.topic_id = p.topic_id
and s.last_post = p.post_date
and s.post_count > 1 --0 if you're using p.post_id up top
inner join users u on
u.id = coalesce(p.user_id, s.orig_poster)
order by
s.last_post desc
This query does introduce coalesce and left join, and they are very good concepts to look into. For two arguments (like used here), you can also use ifnull in MySQL, since it is functionally equivalent.
Keep in mind that that's exclusive to MySQL (if you need to port this code). Other databases have other functions for that (isnull in SQL Server, nvl in Oracle, etc., etc.). I used coalesce so that I could keep this query all ANSI-fied.