I have a SQL code that is pulling information from a database. Here's the code:
$info = "SELECT field_5, record_meta_keywords
FROM cms_custom_database_1
WHERE field_5 > ''
GROUP BY field_5
ORDER BY field_5
DESC LIMIT 10";
The problem is that field_5 has duplicate entries. For example, there are two 868's in that field, but the output is only showing one and not the other. How can I alter this to display all entries and not just one of each number?
You should not use group by without aggregation function
SELECT field_5, record_meta_keywords
FROM cms_custom_database_1
WHERE field_5 > ''
ORDER BY field_5
DESC LIMIT 10
if you want distinct value then use distinct
the use of group by
without aggregation function is deprecated in SQL, not allowed (by default) in most recent version of mysql, and produce unpredictable result with other versions (<5.7)
In your case if you want see all the result then just remove order by
GROUP BY
returns unique (i.e., distinct) occurrences. Take it out of your query statement.
You are explicitly stating -- via the group by
-- that you want only one row in the result set for each value of field_5
.
That leaves you with two options. One is to remove the GROUP BY
:
SELECT field_5, record_meta_keywords
FROM cms_custom_database_1
WHERE field_5 > ''
ORDER BY field_5 DESC
LIMIT 10
The other is to combine the other data into a single column:
SELECT field_5, GROUP_CONCAT(record_meta_keywords) as record_meta_keywords
FROM cms_custom_database_1
WHERE field_5 > ''
GROUP BY field_5
ORDER BY field_5 DESC
LIMIT 10