How to order united request by date? - php

I select 'title' from one table and order by 'date'. I select 'title' from other table and order by 'date'(two columns with same name in two tables). Then I unite them like this.
(SELECT title FROM books WHERE id = :id ORDER BY date DESC) UNION ALL (SELECT title FROM movies WHERE id = :id ORDER BY date DESC);
How can I oder them all together by date?

If you do not select date, you can't; if you add date to the selects, you can just remove the parenthesis, and the first ORDER BY clause. Without parenthesis, MySQL applies ORDER BY to the entire UNION

For ordering a column you must have in select like the sample below
SELECT title, `date` FROM books WHERE id = :id
UNION ALL
SELECT title, `date` FROM movies WHERE id = :id
ORDER BY date DESC
You can sort using order by only columns present in select ..so you need date .. and you can only union table with the same column ( number, type) so the date is in both the select .. and in union the order is needed only at the end of the query

Related

show users the position after sorting based on score

I have a table which has the id column and score column, I want to sort the table based on score column and then find the specific user who is loading the page and show him his/her position. for example, tell him "your position is 40th".
Well I know how to sort a query:
SELECT id,score FROM `table` ORDER BY `score` DESC
But after the sort how can I find an specific id's position?
You don't need an order by for this. Instead:
select 1 + count(*)
from table t
where t.score > (select t2.score from table t2 where id = $id);
Try it:
SELECT #rownum:=#rownum+1 ‘rank’, id, score FROM table t, (SELECT #rownum:=0) r ORDER BY score DESC;
This will create a column and increase 1 in each record.

How to fetch the row from MySQL having latest date

I have a table "poem" with fields "dated","content" etc.
I want to get the content of the recent dated field.
$sql="select content, max(dated) as latestDate from poem";
This is not working.
If you want just one row, use order by and limit:
select p.*
from poem p
order by dated desc
limit 1;
If you want all rows with the most recent date:
select p.*
from poem p
where p.dated = (select max(dated) from poem);
You simply have to order by the date
SELECT * FROM TABLENAME ORDER BY DATE DESC

how to order resultset based on fields from two tables

I have two tables one for topic_likes & one for user_comments.I must get recent updates of like & comment from this tables.Given below is the sql :-
SELECT (required fields...)
LEFT JOIN topic_likes AS TL ON (TL.delete_status=0 AND TL.user_id!=$user_id)
LEFT JOIN user_comments AS UC ON (UC.delete_status=0 AND UC.user_id!=$user_id)
WHERE
(TL.created_date >= '$lastLogin' OR UC.created_date >= '$lastLogin'
ORDER BY UC.created_date desc,TL.created_date desc
LIMIT $limit
I have given order by two fields from two tables(UC.created_date, TL.created_date)
But it does not order the resultset based on created_date from topic_likes.It only orders the results based on user_comments table
But if I removed the limit condition it gives correct results...!!
Any suggestion appreciated
This is a strange approach you're taking. If you want to display user's likes and comments using a single query you should UNION the results. Example:
SELECT * FROM
(
SELECT id, `date`, 'like' as `type` FROM topic_likes
UNION
SELECT id, `date`, 'comment' as `type` FROM user_comments
) a order by a.date DESC limit 5;
The result should be similar to this:
But there are limitations. The number of columns from each subquery must match.

How to count who has most hits in MySQL

I have a table with a column called bid and I would like to list how many records there exists with each bid value.
Kind of making a TOP TEN list.
Example:
bid with value 1 has 5 entries/rows.
bid with value 2 has 3 entries/rows.
bid with value 3 has 8 entries/rows.
etc
How do I make a query that counts and sums up each of the bid's and sort the in a DESCending order?
Thankful for any kind of help!
This should work in MySQL
select u.firstname, t.bid, count(*) as counts
from your_table t
join users u on u.bid = t.bid
where t.confirmed <> '0000-00-00 00:00:00'
group by t.bid
order by counts desc
Generally you can do
select u.firstname, t.bid, t.counts
from
(
select bid, count(*) as counts
from your_table
where confirmed <> '0000-00-00 00:00:00'
group by bid
) t
join users u on u.bid = t.bid
order by t.counts desc
How about this?
SELECT bid, count(*) as TotalHits
FROM tableName
GROUP BY bid
if you want result as per hits sorted, use
SELECT bid, count(*) as TotalHits
FROM tableName
GROUP BY bid
ORDER BY TotalHits DESC

Order by distinct number of rows for 3 tables?

I have 3 tables - News, Content and Edits.
Every row in each table contains a user_id field.
What I want to do is make a list of the top contributors to these 3 tables.
So I need to count how many times each different user_id appears in each table and order from the highest count to the lowest.
Like this:
SELECT
user_id,
COUNT(*) AS all_actions
FROM (
SELECT user_id FROM News
UNION ALL
SELECT user_id FROM Content
UNION ALL
SELECT user_id FROM Edits
) tmp_table
GROUP BY user_id
ORDER BY all_actions DESC
This should do it.
SELECT all_user_ids.user_id as `user_id`, COUNT(all_user_ids.user_id) as `count`
FROM
(SELECT user_id FROM news
UNION ALL
SELECT user_id FROM content
UNION ALL
SELECT user_id FROM edits) AS all_user_ids
GROUP BY `user_id`
ORDER BY `count` DESC
This would be a per table instance
SELECT DISTINCT(COUNT(user_id)) as num FROM news GROUP BY user_id ORDER BY num DESC LIMIT 0,3

Categories