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
Related
This is my table structure
and this is my dataset
What I want is query that gets data ordered by date desc and group by id_patient
so the result in the dataset example should be like this:
I would go with limit clause with subquery since you have PK :
select *
from table t
where id = (select t1.id
from table t1
where t1.id_patient = t.id_patient
order by t1.date desc
limit 1
);
However, if single patient has multiple same dates then this would produce only single records based on date.
SELECT * from rdv a JOIN (SELECT id_patient,MAX(date) date FROM rdv GROUP by id_patient ) b on a.id_patient = b.id_patient and a.date = b.date
If you want the latest record for each patient, then you are not looking for an aggregation. I would often approach this with a correlated subquery:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.id_patient = t.id_patient);
SELECT *
FROM table
GROUP BY group by id_patient
ordered by DATE(date) desc;
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
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.
Issue
I have one table (posts) with articles and article meta.
Another table (post_reviews) contains user-submitted ratings (a value out of 5) for each article, referencing posts by the id of the post in question.
I am trying to find the top three posts, by review, of the last 3 days. Therefore I need to:
find all the posts in that time period (3 days)
find the average rating for each post
sort them by average rating (desc)
Code
For the first part, I can successfully use the query:
SELECT * FROM `posts` WHERE `hub_id`=:hub_id AND `date`>=:start_date AND `date`<=:end_date)
To find each individual post's average rating, I use this query:
SELECT SUM(`review`) AS `total` FROM `post_reviews` WHERE `id`=:id
then get the number of rows from this to work out the average:
SELECT * FROM `post_reviews` WHERE `post_id`=:id
How can I combine these three, or process this data so I can order the posts in a time period by the average rating?
ANSWER
The end result looks like this:
SELECT `posts`.`id`, avg(`post_reviews`.`review`) as `average`
FROM `posts`
JOIN `post_reviews` ON (`posts`.`id`=`post_reviews`.`post_id`)
WHERE `hub_id`=:hub_id
AND `posts`.`date`>=:start_date
AND `posts`.`date`<=:end_date
GROUP BY `post_id`
ORDER BY avg(`review`) desc
Not sure what your hub_id represents, but I assume it's necessary; also assume the key field in Posts is posts.post_id and not posts.id:
SELECT `p`.`id`, avg(`pr`.`review`) AS `average`
FROM `posts` AS `p`
JOIN `post_reviews` AS `pr` ON (`p`.`id`=`pr`.`post_id`)
WHERE `hub_id` =:hub_id
AND `p`.`date` BETWEEN CURRENT_DATE-3 AND CURRENT_DATE
GROUP BY `p`.`id`
ORDER BY avg(`review`) DESC;
See Example: sqlfiddle
Not sure about the syntax for MySQL, but the would need a join and a group by.
Something like....
SELECT post_id. avg(review)
FROM Posts P Inner Join Post_reviews PR on (p.post_id = pr.Post_id)
WHERE `hub_id`=:hub_id AND `date`>=:start_date AND `date`<=:end_date)
Group by Post_id
order by 2 desc
Here is a query that works with sqlfiddle to prove it.
SELECT
p.hub_id
,p.post_id
,p.article
,p.articleMeta
,p.date
,IFNULL(AVG(r.ratings), 0) averageRating
FROM posts p
LEFT JOIN post_reviews r ON
r.post_id = p.post_id
WHERE
hub_id = 1
AND date >= CURDATE() - 3
AND date <= CURDATE()
GROUP BY
p.hub_id
,p.post_id
,p.article
,p.articleMeta
,p.date
ORDER BY
p.date DESC
,averageRating DESC
http://sqlfiddle.com/#!2/39315/1
I like to make an forum teaser for my website. Its easy to just show the latest posts or threads.. I like to get latest threads and posts in the same query, ordered by the last activity. So its going to be ordered by REPLY TO POST date, and THREAD POST date in the same query. I think it has some think to do with how you GROUP it, but I'm not sure.
Tables
threads
id, header, text, date, author
posts
id, text, date, author, thread_id
Example of usage
20 minutes ago - How to make an php/mysql script (2)
17 minutes ago - Pls help me out here (0)
1 hour ago - I need help with PHP (1)
As you see, both answered threads and new threads are on the list. (I need a date of the latest reply or when it was created, header and a count() of replys)
I hope you get, and know how to do this.
Troels
UPDATE:
I have this, and its okay, but i only get threads with replys.
SELECT
threads.*,
posts.*,
(SELECT date FROM posts WHERE thread_id = threads.id ORDER BY date DESC LIMIT 0,1) AS postdate,
(SELECT count(id) FROM threads WHERE thread_id = thread.id) AS replys
FROM
threads,
posts
WHERE
threads.id = posts.thread_id
GROUP BY
thread_id
ORDER BY
postdate DESC,
thread.date
LIMIT
0,15
HOW CAN I DO THIS?
UPDATE
aaaaaaaaaaaaaaaaawwwww Yeah!!!!
I managed to do it myself :-) Took a while to get it right.
SELECT
fisk_debat.id,
fisk_debat.dato,
IF((SELECT count(id) FROM fisk_debat_svar WHERE debatid = fisk_debat.id) < 1, fisk_debat.dato, (SELECT dato FROM fisk_debat_svar WHERE debatid = fisk_debat.id ORDER BY dato DESC LIMIT 0,1)) AS svardato,
fisk_debat.overskrift,
(
SELECT count(fisk_debat_svar.debatid)
FROM fisk_debat_svar
WHERE fisk_debat_svar.debatid = fisk_debat.id
) AS svar
FROM fisk_debat
GROUP BY id
UNION
SELECT
fisk_debat_svar.debatid AS id,
max(fisk_debat_svar.dato) AS dato,
max(fisk_debat_svar.dato) AS svardato,
(
SELECT fisk_debat.overskrift
FROM fisk_debat
WHERE fisk_debat.id = fisk_debat_svar.debatid
) AS overskrift,
(
SELECT count(fisk_debat_svar.debatid)
FROM fisk_debat_svar
WHERE fisk_debat_svar.debatid = id
) AS svar
FROM fisk_debat_svar
WHERE id != id
GROUP BY id
ORDER BY svardato DESC, dato DESC
LIMIT 0,15
If you want to keep the current DB structure, you'll need a Union to get the desired result.
An example can be found at http://www.mysqltutorial.org/sql-union-mysql.aspx
However, I'd still advise to change the structure as explained in the comments to your question.
WHERE clauses only select threads with replies, which is normal. You have to use the LEFT JOIN syntax.
Try this:
SELECT
threads.*,
posts.*,
(SELECT date FROM posts WHERE thread_id = threads.id ORDER BY date DESC LIMIT 0,1) AS postdate,
(SELECT count(id) FROM threads WHERE thread_id = thread.id) AS replys
FROM
threads
LEFT JOIN
posts
ON
threads.id = posts.thread_id
ORDER BY
postdate DESC,
thread.date
LIMIT
0,15