-2

When you do a left join in mysql you get results that look something like this (assume we have a customer table and an order table)

| Customer ID | Order ID |
| 1 | 1
| 1 | 2
| 2 | 3

It can be observed that If a customer has more than 1 order the customer data is duplicated ( in this case the order id 1 is returned twice.)

Is there any way to only retrieve each customer once and still get all matching orders using mysql alone (Ie without some sort of code library/framework automatically converting the data into a 3D array or something similar)

Strawberry
  • 33,750
  • 13
  • 40
  • 57
megaman
  • 1,035
  • 1
  • 14
  • 21
  • 1
    Yes. At this point, 5 minutes in the company of any basic, introductory book or tutorial would be useful . That said, I *would* use application code for this instead. – Strawberry Jul 25 '20 at 23:59
  • Which of the right table rows do you want for a given left table row? PS This is a faq. Please before considering posting read your textbook and/or manual & google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. If you post a question, use one phrasing as title. Reflect your research. See [ask] & the voting arrow mouseover texts. – philipxy Jul 26 '20 at 00:12
  • Does this answer your question? [Select first row in each GROUP BY group?](https://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group) – philipxy Jul 26 '20 at 00:13
  • no - https://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group only tells me how to group by a column in the joined table. that is not what i want to do. – megaman Aug 10 '20 at 16:19
  • No, it tells you how to group then pick one of many rows per group. PS You still haven't clearly said what you want. If you want multiple matching rows to appear as subrows in the result how do you expect there to only be one occurrence of the matching left table row as a subrow? (Rhetorical.) Use enough words, sentences & references to parts of examples to clearly & fully say what you mean. When giving a business relation(ship)/association or table (base or query result), say what a row in it states about the business situation in terms of its column values. – philipxy Aug 10 '20 at 21:24

1 Answers1

0

SELECT * FROM table GROUP BY (CUSTOMER ID)

  • Please explain why this is an answer to the question, don't post code-only "answers". Also this is not valid standard SQL. (Although under a certain mode MySQL allows it.) Please don't answer (or ask) duplicate questions, flag them. This is a very basic question that is an easily found duplicate. See my comments on the question post. – philipxy Jul 26 '20 at 00:35
  • all that does it get one row from BOTH tables. i want 1 row from the left table and all of the rows from the right table without the left table rows being duplicated for every right table row – megaman Aug 10 '20 at 16:15