I've been adding a like feature to an entries database... here's the structure of the DBs:
**Users**
user_id
user_name
etc.
**Entries**
entry_id
entry_content
etc.
**Likes**
user_id
entry_id
(It's a little more complicated than that, there are groups/categories, but that should explain it fine...) Here's the SQL query I'm working with at the moment:
SELECT
entries.*,
DATE_FORMAT(entry_date, "%M %D, %Y") as entry_date,
groups.group_short_name,
users.user_name, users.user_id,
FROM entries
INNER JOIN groups ON groups.group_id = entries.group_id
INNER JOIN users ON users.user_id = entries.user_id
ORDER BY entry_date DESC
I'm trying to also retrieve likes per entry with this query and wondering if it is possible. I've been trying:
COUNT(DISTINCT likes.like_id) as likes
with
LEFT JOIN likes ON likes.entry_id = entries.entry_id
But I don't think that is anywhere near right. Am I way off? Is this possible? Hope it all made sense.
Thanks for the help in advance.
Give your tables aliases, for one..
FROM entries e
Then add a column query:
select e.*, (select count(*) from Likes where entry_id = e.entry_id) as entry_likes
Add:
GROUP BY entries.entry_id
See if that works.
Related
I have two tables (users and posts) and I want to write out all posts (among other things) by one user. I'm thinking I should use a JOIN and WHERE but I get an error for using WHERE.
This is my code:
SELECT username, post, joinDate, title, info FROM users
WHERE userId='18'
JOIN posts ON users.userId=posts.userId
ORDER BY date DESC
I'm new to this and perhaps there is a better way but I can't figure it out atm.
Thankful for all answers!
The JOIN clause comes before the WHERE clause, after the FROM clause. First you join together all the tables you need, then you do your filtering with WHERE. Like this:
SELECT username, post, joinDate, title, info
FROM users
JOIN posts ON users.userId=posts.userId
WHERE users.userId='18'
ORDER BY date DESC
try like below
SELECT u.*,p.*
FROM users u JOIN posts p ON u.userId=p.userId
WHERE u.userId=18
ORDER BY date DESC
where will be after join and 18 is int datatype value so not need single quote for this and use alias for avoiding ambigous column name
SELECT username, post, joinDate, title, info
FROM users
JOIN posts ON users.userId=posts.userId and users.userId='18'
ORDER BY date DESC
if userId is in users table then YES you can use where userId='18'.
If userId is in posts table then it should be userId='18' be in join part.
I cannot get multiple rows of the Posts.body column to show up in the subquery. I DO want subqueries and I haven't figured out a way around this MySQL restraint. Please let me know if you need more information.
SELECT profile_picture, body, post_date, filename, username FROM Posts, Users
WHERE Posts.IDUser = Users.IDUser
AND Posts.body LIKE (SELECT Posts.body FROM Posts WHERE Posts.IDUser = (SELECT Users.IDUser FROM Users WHERE Users.username = 'noah'))
ORDER BY `Posts`.`post_date` DESC;
It's not really clear what the problem is exactly, but from what I can see it seems you need to use IN instead of LIKE:
SELECT profile_picture, body, post_date, filename, username FROM Posts, Users
WHERE Posts.IDUser = Users.IDUser
AND Posts.body IN (SELECT Posts.body FROM Posts WHERE Posts.IDUser = (SELECT Users.IDUser FROM Users WHERE Users.username = 'noah'))
^^ here
ORDER BY `Posts`.`post_date` DESC;
On the other hand the inner SELECTs seem unnecessary as you are already joining Posts and Users so you can probably simplify it a lot using just a join.
What exactly are you trying to select?
Any time you are joining a table to itself, or using it in a subquery, you have to alias it.
So your inner query should be something like
(
SELECT p.body
FROM Posts p
WHERE p.IDUser = something
)
I find it pretty hard to understand what you're trying to do with this query. I have a feeling it could be greatly simplified but I'm not sure what data you are after.
There is a problem with my MySql query. Could you help me out with it?
I have a database as in the picture above, and there are userids that belongs to each user. It should be sorted by type descending. It records the time with the PHP time() function which is written at that moment in the datetime cell within the database. However, the SQL query which I used gives me a result as in the picture below:
SELECT
user.username,
kim.userid,
kim.subject,
count(kim.userid),
max(kim.dateline),
max(kim.rateid)
FROM test as kim
INNER JOIN user
WHERE user.userid = kim.userid
GROUP BY kim.userid limit 10)
Although I get the right results, I’m still having a little problem since the rateid does not show the right subject. So, I’ve been searching for it for two days, and I think there’s a problem that I don't understand or cannot see.
The right query should be like the on in the following picture:
I appreciate if you help!
You are trying to get information from the line that has the maximum rateid, along with aggregated information. For this, you need to join back to the original table:
select kim.userid, u.username, kim.cnt, kim.maxdate, kim.maxrate, t.subject
from (SELECT kim.userid, count(kim.userid) as cnt,
max(kim.dateline) as maxdate, max(kim.rateid) as maxrate
FROM test kim
GROUP BY kim.userid
limit 10
) kim INNER JOIN
user u
on u.userid = kim.userid join
test t
on kim.userid = t.userid and
kim.maxrate = t.rateid
This finds the maxrate for each user and then joins back to the table to get the subject for that rate. Your maxdate column seems to be what you want. If you want the date for the maxrate then you might also want to take it from t.
try this
Select user.username, kim.userid ,kim.subject, count(kim.userid), max(kim.dateline),
max(kim.rateid)
from test as kim
left join user ON user.userid = kim.userid group by kim.userid limit 10
Thanks for attention ,
i found the solution of my problem. The solution should be like this:
SELECT subject FROM test WHERE
userid=user.userid ORDER BY dateline desc LIMIT 1)
AS subject,
(SELECT COUNT(*) FROM test
WHERE userid=user.userid) AS adet
FROM user limit 10
I'm fairly new to MYSQL!
I need to make a SQL query where i check how many likes a row has (between two tables)
I found another question that looked like mine, but i can't get it to return anything (even though it doesn't create an error.
query:
SELECT *
FROM likes
INNER JOIN (SELECT likes.like_id,
COUNT(*) AS likes
FROM likes
INNER JOIN uploads ON likes.upload_id=uploads.upload_id
WHERE uploads.upload_date >= DATE_SUB(CURDATE(), INTERVAL 8 DAY)
GROUP BY uploads.upload_id) x ON x.like_id = likes.like_id
ORDER BY x.likes DESC
Link to the original question:
MySQL, Need to select rows that has the most frequent values in another table
Help is much appreciated
Kind regards,
Mathias
Since you didn't post your table structure I'll have to guess..
select someid, count(*) cnt from
(
select * from table1 t1 join table2 t2 on t1.someid = t2.someid
) as q0 group by someid order by cnt desc;
It will need tweaking to fit your schema.
I have a users table and a comments table and i want select from users the top users that have the big amount of comments from the comments table and order them by numbers of comments
table structure
users
id | username | password
comments
id | text | author_username
Use the following MySQL statement to list the users with the most comments. CommentCount tells you the number of comments made by a particular user.
SELECT
users.username,
COUNT(comments.id) AS CommentCount
FROM
users
INNER JOIN comments ON users.id = comments.author_userid
GROUP BY
users.username
ORDER BY
COUNT(comments.id) DESC
Please note that you will have to change author_userid into author_username first!
My SQL is a bit rusty, but something like this should give you what you're looking for (although as I mentioned, the user ID should be the only user identifier in the comments table.)
Select count(id), author_username from comments group by author_username
select u.username,count(c.comments) as total
from users as u
left join comments as c
on u.username = c.author_username
group by u.username
order by total desc
I would change join field as dutchie432 suggested. Add a limit clause in order to have your desired number of records.