mysql - SELECT COUNT messes with ORDER BY -
mysql - SELECT COUNT messes with ORDER BY -
i have problem don't understand. query
select f.*, ftv.content, ftv.updated, count(ftv.file_number) versions files f inner bring together files_text_versions ftv on ftv.file_number = f.file_number f.file_number = '%s' order ftv.updated desc limit 1
totally ignores order statement, if remove the
count(ftv.file_number) versions
the query runs fine , orders should.
why that, , how prepare it?
sql fiddle same problem 1 table:
http://sqlfiddle.com/#!2/c8f124/2
your count(*)
turns query aggregation query. without group by
, returns 1 row. although in other databases error, mysql allows syntax.
you can prepare adding group by
. however, have problem other 2 columns ftv
. can values want clever aggregation:
select f.*, substring_index(group_concat(ftv.content order ftv.updated desc), ',', 1) content, max(ftv.updated) updated, count(ftv.file_number) versions files f inner bring together files_text_versions ftv on ftv.file_number = f.file_number f.file_number = '%s' grouping t.file_number order updated desc limit 1;
mysql
Comments
Post a Comment