MySQL query usage of Order by ASC and DESC - mysql

I have this query:
SELECT * FROM project as p inner join users as u on p.user_id = u.user_id
ORDER BY p.projectname, u.date_joined
Now what I want to ORDER by projectname ASC and u.date_joined DESC.
How could I make it this way? I tried this code here but doesn't work:
SELECT * FROM project as p inner join users as u on p.user_id = u.user_id
ORDER BY p.projectname ASC, u.date_joined DESC
Order the projectname ascending works but the descending won't work.

To order project name descending, do this:
SELECT *
FROM project p
INNER JOIN users u ON p.user_id = u.user_id
ORDER BY p.projectname DESC, u.date_joined DESC
This will order your result by projectname desc (Z to A) first. For identical projectname records, date_joined will be used for sorting further (latest date first).

Related

Retrieve last 10 results ASC Left Join MySql

I'm trying to retrieve the last 10 posts from a posts table ordered ASC, but the last left joined query doesn't retrieve anything.
Basic: it retrieves results ordered DESC
SELECT
p.post, p.id_post, u.name
FROM
posts p
LEFT JOIN
users u ON u.id_user = p.id_user
WHERE
p.id_user = 4
ORDER BY
p.date DESC
LIMIT 10
Ordered ASC: it doesn't work at all:
SELECT
num.*
FROM
(SELECT
p.post, p.id_post, u.name
FROM
posts p
LEFT JOIN
users u ON u.id_user = p.id_user
WHERE
p.id_user = 4
ORDER BY
p.date DESC
LIMIT 10) num
ORDER BY
p.date ASC
What am I doing wrong?
In my php variables I use $row['id_post'] $row['post'] $row['name']. I don't want to use array_reverse() just plain sql
You need to return the date in the subquery:
SELECT pu.post, pu.id_post, pu.name
FROM (SELECT p.*, u.name
FROM posts p LEFT JOIN
users u
ON u.id_user = p.id_user
WHERE p.id_user = 4
ORDER BY p.date DESC
LIMIT 10
) pu
ORDER BY pu.date ASC;
Your second query should have returned an error message to the effect that date is not recognized as a column. You should be capturing and reading error messages, if you want to write an effective application.

MySQL subquery with LIMIT alternative

What I want:
SELECT u.username, u.last_activity
FROM users_userprofile
WHERE u.id IN (
SELECT DISTINCT(p.user_id) FROM forums_post p
WHERE p.thread_id = 423993
ORDER BY p.created_at DESC
LIMIT 4
);
This doesn't work because of LIMIT in subquery. I want to keep order of subquery but I want to get username and last_activity instead of user_id.
Any suggestion how I could achieve this?
Replace the subquery with a view:
CREATE VIEW subv AS SELECT p.user_id FROM forums_post p
WHERE p.thread_id = 423993
ORDER BY p.created_at DESC
LIMIT 4;
SELECT u.username, u.last_activity
FROM users_userprofile
WHERE u.id IN (SELECT * FROM subv);
you could use join for the table and the subquery instead of using where in:
SELECT u.username, u.last_activity
FROM users_userprofile u
JOIN (
SELECT p.user_id FROM forums_post p
WHERE p.thread_id = 423993
ORDER BY p.created_at DESC
LIMIT 4
) q
on u.user_id=q.user_id
Why woldn't you do it with a JOIN? There seems to be no performance impact because WHERE and LIMIT clauses are the same. It won't JOIN the whole tables:
SELECT p.user_id, u.username, u.last_activity
FROM users_userprofile u
JOIN forums_post p ON p.user_id = u.id
WHERE p.thread_id = 423993
GROUP BY p.user_id ORDER BY MAX(p.created_at) DESC
LIMIT 4

ORDER BY MySQL and timestamp

I would like to order my posts by their timestamp but that doesn't and I don't know why.
When I execute my query without order, it works, but when I add ORDER BY it doesn't work.
This is my query :
SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic LIMIT :limit_down, :limit_up
ORDER BY Posts.datePosts DESC;
Why that doesn't work?
Try inversing the ORDER BY/LIMIT:
SELECT * FROM Posts
INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic
ORDER BY Posts.datePosts DESC
LIMIT :limit_down, :limit_up;
The order by needs to be before the limit, else the limit is way less useful: you'd get the X first rows of the table, but "first" using what criteria? And then you'd order those results.
You can do that, too, if you really want to, but think about what you are doing then:
SELECT * FROM (
SELECT Posts.datePosts orderField, * FROM Posts
INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic
LIMIT :limit_down, :limit_up;
)
ORDER BY orderField DESC
Try this:
SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic
ORDER BY Posts.datePosts DESC
LIMIT :limit_down, :limit_up;
Limit must be the last thing applied to a query, so order by needs to come first.
Seems dateposts is a date field then why not try this
SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic LIMIT :limit_down, :limit_up
ORDER BY DATE(Posts.datePosts) DESC;

mysql multiple fields from subquery

SELECT
(SELECT date FROM forums WHERE topic_id=f.id OR id=f.id ORDER BY id DESC LIMIT 1) as last_reply,
f.*, p.id as pid, p.name FROM forums f
INNER JOIN players p ON p.id = f.author
WHERE f.topic_id=0 ORDER BY f.id DESC
In the subquery, I'd like to return not only the date field, but also the author field as well. how can I do this?
looked at a similar post but can't apply it to mine.
I would do it something like this:
SELECT
(SELECT date FROM forums WHERE topic_id=f.id OR id=f.id ORDER BY id DESC LIMIT 1) as last_reply,
(SELECT author FROM forums WHERE topic_id=f.id OR id=f.id ORDER BY id DESC LIMIT 1) as last_author,
f.*, p.id as pid, p.name FROM forums f
INNER JOIN players p ON p.id = f.author
WHERE f.topic_id=0 ORDER BY f.id DESC
I would actually repeat the subquery again
Maybe something like this can help you :
SELECT MAX(f.date), f.author
FROM forums f
INNER JOIN players p ON
p.id = f.author
WHERE f.tpoic_id = 0
GROUP BY f.author
ORDER BY f.id DESC
But it's difficult with no structure of tables.
Good luck.

problem in mysql query with join and limit the time

i have this
SELECT COUNT(1) cnt, a.auther_id
FROM `posts` a
LEFT JOIN users u ON a.auther_id = u.id
GROUP BY a.auther_id
ORDER BY cnt DESC
LIMIT 20
its work fine
bu now i want select from posts which added from 1 day tried to use
WHERE from_unixtime(post_time) >= SUBDATE(NOW(),1)
but its didnot worked
any one have idea
My guess is that you added the WHERE clause in the wrong place. It should come after the JOIN but before the GROUP BY, like this:
SELECT COUNT(1) cnt, a.auther_id
FROM `posts` a
LEFT JOIN users u ON a.auther_id = u.id
WHERE from_unixtime(post_time) >= SUBDATE(NOW(),1)
GROUP BY a.auther_id
ORDER BY cnt DESC
LIMIT 20