1

I have a table called monitor_links with the following columns:

Image of my Table

This table is used to store links from others websites. I'm getting those links using this query:

SELECT title, url
FROM monitor_links
WHERE id_domain = 5
GROUP BY url 
ORDER BY date_created asc

With that query I return the title and url from the storage link (where id_domain is equal to 5). But in my case, can exists rows with same url, and to not return same repeated url I'm using GROUP BY url. (If exists more than one row with same URL and id_domain = 5, my code returns only one of this rows).

The problem is that I'm getting the following error:

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shuri.monitor_links.title' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by'.

Some people said that I can try to disable the only_full_group_by, but I want to fix my query rather than silencing the warning.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sudo Sur
  • 385
  • 4
  • 17

2 Answers2

4

You can use the ANY_VALUE function to tell MySQL that you want to return any value within the group.

SELECT ANY_VALUE(title) AS title, url
FROM monitor_links
WHERE id_domain = 5
GROUP BY url

This is essentially equivalent to turning off ONLY_FULL_GROUP_BY temporarily for this query.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

If you want one title for each url, then:

SELECT MAX(title) as title, url
FROM monitor_links
WHERE id_domain = 5
GROUP BY url ;

If you want the most recent version:

SELECT ml.*
FROM monitor_links ml
WHERE ml.created_date = (SELECT MAX(ml2.created_date)
                         FROM monitor_links ml2
                         WHERE ml2.url = ml.url AND ml2.id_domain = ml.id_domain
                        );
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786