简体   繁体   中英

mysql left join and order by

hi i have to different tables m_mp and m_answer both have m_date and m_mdate which fills with time() function i want to select both entries but order by date

example:

first table: 'some text','6464647776' second table 'some answer','545454545'

so i want to show first second table and then the first one

this is the code im using:

SELECT r.*,u.* 
FROM `mensajes` as r 
LEFT JOIN `m_answer` as u on r.id = u.id_m 
WHERE r.id  = '1' 
ORDER BY m_date

and then display the result of each table using while-loop

I guess I get what you want to do.

You may force both grouping and ordering using ORDER BY, like this:

http://sqlfiddle.com/#!9/373d65/9

I know the solution is not optimal in terms of speed, but, given proper indices, I suspect performance to be acceptable, unless you are already aiming for millions of messages; when that time comes, you would like to either properly GROUPBY, or make subsequent queries for just the recently answered questions from the current page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM