Different result query when use mysql and mariadb

自闭症网瘾萝莉.ら 提交于 2020-01-16 04:07:11

问题


Here is my problem:

My database have table Book, Post. Each book has many post

Table posts has field 'book_id', that is foreign key reference table Book primary key (id).

This is my index page. The idea is to get latest post from each book and order by published date.

When I code on localhost, every thing is OK. I can get latest post from each book and order by publish date. But when I deploy it in vps. It didn't get latest post, it get first post from each book. I didn't have any experience about it. Please help, thanks

On localhost, I use: Apache-2.2, PHP-5.3, Mysql-5.5, ENGINE type for table is InnoDB.

On VPS, I use: Nginx 1.7.6, PHP-FPM 5.5.18, MariaDB, ENGINE type for table is MyIsam

I guest the problem is InnoDB and MyIsam, I try to fix it. But, if you have free time, please give me some good advise. Thanks a lot

p/s: Sorry about my poor english

SELECT * FROM `my_book_store`.`books` 
AS `Book` 
INNER JOIN 
(
    SELECT * 
    FROM posts 
    WHERE posts.published = 1 AND posts.published_date <= NOW() 
    ORDER BY posts.published_date DESC
) AS `Post` 
ON (`Post`.`book_id` = `Book`.`id`) 
WHERE 1 = 1 
GROUP BY `Book`.`id` 
ORDER BY `Post`.`published_date` desc 
LIMIT 100   

回答1:


Try this:

SELECT b.*, p.*  
FROM my_book_store.books AS b 
INNER JOIN posts p ON b.id = p.book_id
INNER JOIN (SELECT p.book_id, MAX(p.published_date) published_date
            FROM posts p 
            WHERE posts.published = 1 AND posts.published_date <= NOW() 
            GROUP BY p.book_id
          ) AS p1 ON p.book_id = p1.book_id AND p.published_date = p1.published_date 
GROUP BY b.id 
ORDER BY p.published_date DESC 
LIMIT 100   



回答2:


You can try the below queries which does the job of getting the last post from each book

select
b.id,
b.name,
p.content,
p.published_date
from book b 
join post p on p.book_id = b.id
left join post p1 on p1.book_id = p.book_id and p1.published_date > p.published_date
where p1.id is null;

OR

select 
b.id,
b.name,
p.content,
p.published_date
from book b 
join post p on p.book_id = b.id
where not exists(
  select 1 from post p1 
  where p.book_id = p1.book_id
  and p1.published_date > p.published_date
)

DEMO




回答3:


The problem seems to be that you're only grouping by Book.id but select a lot of other non-aggregated values, so actual query results depend on the execution plan the optimizer came up with. See also

MySQL extends the use of GROUP BY so that the select list can
refer to nonaggregated columns not named in the GROUP BY clause.
[...]
However, this is useful primarily when all values in each
nonaggregated column not named in the GROUP BY are the same for each group.

THE SERVER IS FREE TO CHOOSE ANY VALUE from each group, so unless they are the same, the values chosen are indeterminate.
Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause.

Different result query when use mysql and mariadb



来源:https://stackoverflow.com/questions/27314820/different-result-query-when-use-mysql-and-mariadb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!