[SQL brush question] When querying three tables, use inner joinVS direct connection query of three tables

Return the customer name and related order number and the total price of each order [directly select the corresponding field in the three tables]

Niuke SQL97 returns the customer name and related order number and the total price of each order.
Question: In addition to returning the customer name and order number, return the customer name (cust_name) in the Customers table and the related order number (order_num) in the Orders table, add The third column, OrderTotal, contains the total price of each order and sorts the results in ascending order by customer name and order number.
insert image description here

SELECT
    a.cust_name,
    c.order_num,
    SUM(c.quantity * c.item_price) AS OrderTotal
FROM
    Customers a,
    Orders b,
    OrderItems c
WHERE
    a.cust_id = b.cust_id
    and b.order_num = c.order_num
GROUP BY
    a.cust_name,
    b.order_num
ORDER BY
    a.cust_name,
    b.order_num

Here GROUP BY requires two fields, what if there is only one field? ?
----The reason for grouping by customer number: each customer corresponds to an order. We calculate the total price according to the order grouping, and naturally we also need to group the customers to find the customers corresponding to each order; —to
put it more intuitively, it is because the customer name cannot be selected in the select without grouping the customers, so we are forced to Group by customer.

Some summary of group by and select:

If there is a group by operation, there are only two types of result set fields after select: or onlyFields appearing after group by, or it appears after group byfield + aggregate functionThe combination

(Five commonly used aggregation functions: min() to find the minimum value in the column, max() to find the maximum value in the column, avg() to find the average value, sum() to find the sum of the values ​​corresponding to the fields in the column, count() to find the column the total number of)

An example of an error

SELECT is_pause,user_id,pause_type,count(stu_id)
FROM call_task
WHERE is_pause='1'
GROUP BY is_pause,pause_type    //在分组中并没有user_id这个字段

As shown in the figure below, although there is no problem with the data in the columns of pause_type and the total number of students, the column of user_id is obviously wrong. Why does this happen? ? ? Because we only use the two fields is_pause and pause_type as grouping conditions when grouping , SQL will only group the data of these two columns in the original database, and other columns will not participate in the grouping process (note !!) , but when displaying the result set, you have to let it display the columns that have not been grouped. The last way it adopts is to select the user_id of the first set of data that appears when the where and grouping conditions are met.

insert image description here

In fact, the idea of ​​grouping is very simple: Take the above example as an example, think of the grouping condition as a two-dimensional array, and add 1 to the number if the array is satisfied. (is_pause, pause_type) is a simple two-dimensional array, combined with filter conditions and data in the database, (is_pause, pause_type) is divided into the following three situations: (1,2), (1,4), (1 ,5), the value of count (stu_id) that satisfies the array condition is incremented by 1. In the same way, if your grouping condition is 3, 4 or more, think in this way.
Reference from: blog

Another way to determine the best customer [INNER JOIN==select directly in 3 tables]

Niuke SQL100 Another way to determine the best customer (2)
Question: Write a SQL statement to return the name and total value of the customer whose total order price is not less than 1000 (order_num in the OrderItems table).
Hint: A sum needs to be calculated (item_price multiplied by quantity). To sort the results by total, use the INNER JOIN syntax.
insert image description here

SELECT
    cust_name,
    total_price
FROM
    Customers c
    INNER JOIN Orders o ON c.cust_id = o.cust_id
    INNER JOIN (
        SELECT
            order_num,
            SUM(item_price * quantity) AS total_price
        FROM
            OrderItems
        GROUP BY
            order_num
        HAVING
            total_price >= 1000
    ) t ON t.order_num = o.order_num
ORDER BY
    t.total_price ASC
SELECT
    cust_name,
    SUM(item_price * quantity) AS total_price
FROM
    OrderItems a,
    Orders b,
    Customers c
WHERE
    a.order_num = b.order_num
    and b.cust_id = c.cust_id
GROUP BY
    cust_name
HAVING
    sum(item_price * quantity) >= 1000
order by
    sum(item_price * quantity)

Guess you like

Origin blog.csdn.net/weixin_43629813/article/details/129483492