Issue with SQL statement in MySQL and MariaDB

I have the below SQL statement which works as expected on my local machine WAMP server using MySQL, but when I upload to the live server using MariaDB, it doesn't work as expected anymore. The SQL statement is used for post and comment display. Any post or comment with latest postdate should be on top of the displayed list. As I said, it worked perfectly with MySQL on my local WAMP server, but when I upload to live server using MariaDB, no matter what I tried, it reverses the order by showing latest post/comment at the bottom of the list and old post/comments at the top. Please help me as I'm now confused on why it's behaving this way.

The SQL statement is shown below:

SELECT * FROM 
(SELECT once.id, message, audio, audio_desc, accepted, postdate, tag, broadcast, once.mobile, once.username1, state, users.username, users.firstname, users.lastname, users.fullname, users.avatar, users.alias, useroptions.aliascheck, content_provider.content_type, comment.comment_date as c1 FROM once LEFT OUTER JOIN users ON once.username1 = users.username INNER JOIN useroptions ON once.username1 = useroptions.username LEFT OUTER JOIN content_provider ON once.username1 = content_provider.provider LEFT JOIN comment ON once.id = comment.post_id AND once.tag = comment.post_tag WHERE once.username1='logusername' OR once.username1='myusername'
UNION ALL
SELECT daily.id, message, audio, audio_desc, accepted, postdate, tag, broadcast, daily.mobile, daily.username1, state, users.username, users.firstname, users.lastname, users.fullname, users.avatar, users.alias, useroptions.aliascheck, content_provider.content_type, comment.comment_date as c1 FROM daily LEFT OUTER JOIN users ON daily.username1 = users.username INNER JOIN useroptions ON daily.username1 = useroptions.username LEFT OUTER JOIN content_provider ON daily.username1 = content_provider.provider LEFT JOIN comment ON daily.id = comment.post_id AND daily.tag = comment.post_tag WHERE daily.username1='logusername' OR daily.username1='myusername'
UNION ALL
SELECT weekly.id, message, audio, audio_desc, accepted, postdate, tag, broadcast, weekly.mobile, weekly.username1, state, users.username, users.firstname, users.lastname, users.fullname, users.avatar, users.alias, useroptions.aliascheck, content_provider.content_type, comment.comment_date as c1 FROM weekly LEFT OUTER JOIN users ON weekly.username1 = users.username INNER JOIN useroptions ON weekly.username1 = useroptions.username LEFT OUTER JOIN content_provider ON weekly.username1 = content_provider.provider LEFT JOIN comment ON weekly.id = comment.post_id AND weekly.tag = comment.post_tag WHERE weekly.username1='logusername' OR weekly.username1='myusername') outerQueryName GROUP BY id ORDER BY COALESCE(GREATEST(postdate, MAX(c1)), postdate) DESC

Open in new window

Adebayo OjoRegional Network ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Did you check whether ONLY_FULL_GROUP_BY is set or not on both servers?

Also I'm not sure whether your GROUP BY/ORDER BY can be deterministic at all.
PortletPaulEE Topic AdvisorCommented:
GROUP BY
MySQL introduced a really bad idea. It has led folk to believe that you only need to have one or a few columns in a group by clause, and as if by magic that the remaining columns will sort themselves out. Like most magic, this is an allusion. To avoid this problem repeat all "non-aggregating" columns of the select clause into the group by clause.

Ste5an has (correctly) pointed out that the only_full_group_by setting is probably different in the 2 environments, but I would add that even if they are changed to be the same, you still need to avoid this pitfall. Always use all "non-aggregating" columns of a select clause in the group by clause.

 (If there is a "deterministic" relationship strictly speaking this isn't needed, but as most of us never know if it is fully "deterministic" or not, the safe approach is to specify all grouping columns.)

Here, because you are NOT using any aggregations, you can achieve unique rows by using "select distinct" (if really necessary).

[edit]
with the advent of MySQL 8 using the window function row_number() may become useful to you (to get just the most recent rows). row_number() has been available in MariaDB for some time now, but is quite recent for MySQL.
Adebayo OjoRegional Network ManagerAuthor Commented:
@Ste5an, how do I do that? I'm a novice when it comes to databases.

@PortLetPaul I'm confused the more now with your explanation. Please can you just give a correct SQL statement that would work on mariadb?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

PortletPaulEE Topic AdvisorCommented:
Always use all "non-aggregating" columns of a select clause in the group by clause.

select id, a, b, c, d, e, count(*)
from x
group by id, a, b, c, d, e

You can't just group by id
=========================

My best guess on the query is to not use group by at all, I'm not confident you even need "select distinct", it really comes down to the order by clause. I assume postdate is mandatory, but comment date is not mandatory. It really isn't clear to me.
SELECT
    *
FROM (
    SELECT
        once.id
      , message
      , audio
      , audio_desc
      , accepted
      , postdate
      , tag
      , broadcast
      , once.mobile
      , once.username1
      , state
      , users.username
      , users.firstname
      , users.lastname
      , users.fullname
      , users.avatar
      , users.alias
      , useroptions.aliascheck
      , content_provider.content_type
      , comment.comment_date AS c1
    FROM once
    LEFT OUTER JOIN users ON once.username1 = users.username
    INNER JOIN useroptions ON once.username1 = useroptions.username
    LEFT OUTER JOIN content_provider ON once.username1 = content_provider.provider
    LEFT JOIN comment ON once.id = comment.post_id
    AND once.tag = comment.post_tag
    WHERE once.username1 = 'logusername'
    OR once.username1 = 'myusername'
    UNION ALL
    SELECT
        daily.id
      , message
      , audio
      , audio_desc
      , accepted
      , postdate
      , tag
      , broadcast
      , daily.mobile
      , daily.username1
      , state
      , users.username
      , users.firstname
      , users.lastname
      , users.fullname
      , users.avatar
      , users.alias
      , useroptions.aliascheck
      , content_provider.content_type
      , comment.comment_date AS c1
    FROM daily
    LEFT OUTER JOIN users ON daily.username1 = users.username
    INNER JOIN useroptions ON daily.username1 = useroptions.username
    LEFT OUTER JOIN content_provider ON daily.username1 = content_provider.provider
    LEFT JOIN comment ON daily.id = comment.post_id
    AND daily.tag = comment.post_tag
    WHERE daily.username1 = 'logusername'
    OR daily.username1 = 'myusername'
    UNION ALL
    SELECT
        weekly.id
      , message
      , audio
      , audio_desc
      , accepted
      , postdate
      , tag
      , broadcast
      , weekly.mobile
      , weekly.username1
      , state
      , users.username
      , users.firstname
      , users.lastname
      , users.fullname
      , users.avatar
      , users.alias
      , useroptions.aliascheck
      , content_provider.content_type
      , comment.comment_date AS c1
    FROM weekly
    LEFT OUTER JOIN users ON weekly.username1 = users.username
    INNER JOIN useroptions ON weekly.username1 = useroptions.username
    LEFT OUTER JOIN content_provider ON weekly.username1 = content_provider.provider
    LEFT JOIN comment ON weekly.id = comment.post_id
    AND weekly.tag = comment.post_tag
    WHERE weekly.username1 = 'logusername'
    OR weekly.username1 = 'myusername'
) outerQueryName
ORDER BY
    GREATEST(COALESCE(c1,postdate), postdate) DESC

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Adebayo OjoRegional Network ManagerAuthor Commented:
Thanks PortletPaul, that works as expected.
PortletPaulEE Topic AdvisorCommented:
Great, pleased we could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.