课程: SQL Practice: Intermediate Queries

Solution: List items in each order - SQL教程

课程: SQL Practice: Intermediate Queries

Solution: List items in each order

- [Instructor] To solve this challenge, I used GROUP_CONCAT, a function in MySQL and similar DBMSs that concatenates items based on their grouping. Microsoft SQL has a similar function called STRING_AGG, short for string aggregation, that works in the same way. If you use that, you'll need to have provided a separator, which to make the output match with the challenge request, should be a comma with no space following it. This function works together with a grouping term, which in this case is GROUP BY OrderID. Here we can see the different versions of MySQL and MS SQL syntax that have the same result that we're looking for. The challenge also asked for the list of items to be sorted alphabetically, and here with this MySQL syntax, I'm doing that with the ORDER BY D.Name in the GROUP BY function. Ascending is the default order here, so I can leave that off. But if I wanted to be explicit, I could add ASC as well. With MS SQL, that would be a bit different, including A within GROUP clause where the order by D.Name would be. Back here in my solution, we can see that I've joined the three provided tables, Dishes, OrdersDishes, and Orders, so that I can resolve an order ID to the names of the dishes included in it. And as the challenge asked for, I'm only interested in the orders from May 1st, 2022, or 2022-05-01 and later. Then, when these names are returned, they're listed altogether, in alphabetical order, separated by commas, by each order ID. Let's see if my solution works. I'll click Test my code, and it looks like my output is correct.

内容