last day (15 days later) » 

11:47 AM
-1
Q: MySQL - LEFT JOIN only displays one data

Suluh SI have the MySQL query as follows: SELECT articles.*, users.*, COUNT(comment_id) AS comment_total, categories.* FROM articles LEFT JOIN users ON post_by = user_id LEFT JOIN comments ON post_id = comment_post_id AND comment_status = 1 LEFT JOIN categories ON post_cat_id = category_id WHERE ...

 
why you use count without group by?
 
I don't understand left join :( I just learned PHP framework and it turns out I have to use the left join so it's simple
 
To add to @fa06 's comment This query doesn't make anny sense add all without GROUP BY in fact it should error.. See manual as you are not allowed in ANSI/ISO standard SQL to mix aggregate functions with nonaggregated columns without GROUP BY well a exception on that rule would be functional dependence from the ANSI/ISO SQL 1999 standard
See Why should I provide a Minimal Reproducible Example for a very simple SQL query? for providing example data and expected results..
 
oh well, this works, thanks >_<
 
This is not clear. Use enough words, sentences & references to parts of examples to clearly & fully say what you mean. But it seems extremely likely that this is a certain faq. Before considering posting please always google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names; read many answers. If you post a question, use one phrasing as title. See How to Ask & the voting arrow mouseover texts. But when you do have a non-duplicate question--
Please in code questions give a minimal reproducible example--cut & paste & runnable code; example input with desired & actual output (including verbatim error messages); tags & clear specification & explanation. That includes the least code you can give that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.)
Learn what LEFT JOIN ON returns: INNER JOIN ON rows UNION ALL unmatched left table rows extended by NULLs. Always know what INNER JOIN you want as part of an OUTER JOIN. A WHERE or INNER JOIN ON that requires a right [sic] table column to be not NULL after an OUTER JOIN ON removes any rows extended by NULLs, ie leaves only INNER JOIN ON rows, ie "turns OUTER JOIN into INNER JOIN". You have that.
@RaymondNijland An aggregate and/or having without group by is perfectly normal & implicitly groups on all columns.
@fa06 An aggregate and/or having without group by is perfectly normal & implicitly groups on all columns.
 
11:47 AM
@philipxy i would suggest then (re)reading the manual how GROUP BY should work as you are wrong. the manual says that SELECT name, MAX(age) FROM t for example is invalid, which is the same case as this query.
 
@RaymondNijland That manual page is not saying the max() or aggregating without explicit grouping is wrong. It is saying that it is wrong to select a column not in an aggregate function when it is neither a (explict or implicit) grouping column nor functionally dependent on one. What my comment said is this: "If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows." So "This query doesn't make any sense at all without GROUP BY in fact it should error" is wrong.
 
@philipxy without table structures it is impossible to know which off of us is right remember i mentioned the exception rule functional dependence which i mentioned in mine first comment.... functional dependence is the only valid way for the query without this query does not make sense...
 
@RaymondNijland Again: Without table structures & with a group by it is impossible to know whether non-aggregated non-group by column use is ok/wrong. But without a group by if there is an aggregate then there is implicit grouping on all columns so you can use any column either aggregated or not. (Aggregation is over all rows.)
 
@philipxy it's clear we are talking past one and other for 15 minutes now, Show a (simple) sqlfiddle with your "implicit grouping" case.. As trusting "implicit grouping" simply sounds like playing russian roulette with your result set.
 
@RaymondNijland I have not missed what you are saying, I have consistently been clear & echoed what you are saying & even related that to the answer while pointing out that you are not reading what I am writing. Yes, run some code & find out that you can use aggregates without a group by clause & there is no non-determinism or violation of the SQL standard--it has nothing to do with the MySQL non-standard non-deterministic non-ONLY_FULL_GROUP_BY.
 
11:47 AM
"Yes, run some code & find out that you can use aggregates without a group by clause." @philipxy i rather not do, the only valid "implicit grouping" which i will trust is SELECT SUM(age) FROM t.. as far as i know SELECT name, MAX(age) FROM t is invalid ANSI/ISO SQL 1992 standard wise and should error plain and simple, PostgreSQL also does it correct .. Which bring us back to SELECT articles.*, users.*, COUNT(comment_id) which is the same thing..
@philipxy "ANSI/ISO standard SQL to mix aggregate functions with nonaggregated columns without GROUP BY well a exception on that rule would be functional dependence from the ANSI/ISO SQL 1999 standard" The only wrong part of this statement is the functional dependence part that was a mistake.. As you can't use functional dependence on SELECT name, MAX(age) FROM t i just realize.
 
@RaymondNijland Here is PostgreSQL on my original claim: "If a query contains aggregate function calls, but no GROUP BY clause, grouping still occurs: the result is a single group row." Fiddle: SELECT MAX(age) FROM t PS Unfortunately I wrongly described this as implicitly grouping on "all columns"--correct is, no columns. Then thinking of all columns being selected I wrongly said "you can use any column either aggregated or not"--correct is, any columns aggregated. My apologies.
@RaymondNijland PS Since "the result is a single group row" & MySQL allowed non-aggregated columns, presumably my point explains the title: "MySQL - LEFT JOIN only displays one data". ("data" presumably meaning "row") (No minimal reproducible example of course.) Thanks for persevering.
 
"My apologies" @philipxy no problem making mistakes will happen "Since "the result is a single group row" & MySQL allowed non-aggregated columns, " If that is the case then MySQL should also allow SELECT name, MAX(age) FROM t LIMIT 1 with sql_mode=only_full_group_by As that is also forced to be a single row result set right?
 
@RaymondNijland I don't understand what point you are trying to make. Guessing... The facts that LIMIT 1 returns 1 row & all FDs hold in a 1-row table value could be but are not used by the SQL standard for deducing functionally dependent columns. Can MySQL be convinced of functional dependency when HAVING COUNT(*) = 1? PS Only disppointment can come from "SQL should".
 
yes the ANY_VALUE() could be used but then you agree playing russian roulette with MySQL as the results are still nondeterministic (random) as you tell MySQL to ignore the nondeterminism @philipxy i would love to spar futher but we already moved way to much offtopic i think..
 

last day (15 days later) »