UPDATE 테이블 조인
- 업데이트 하려는 테이블과 다른 테이블을 조인한 결과를 업데이트할 경우
UPDATE
tree AS main,
(
SELECT @rownum:=@rownum+1 AS no, t.id, t.name …
UPDATE
tree AS main,
(
SELECT @rownum:=@rownum+1 AS no, t.id, t.name …
Input:
Visits
+----------+-------------+
| visit_id | customer_id |
+----------+-------------+
| 1 | 23 |
| 2 | 9 |
| 4 | 30 |
| 5 | 54 |
| 6 | 96 |
| 7 | 54 |
| 8 | 54 |
+----------+-------------+
visit_id is the primary key for this table.
This table contains information about the customers who visited the mall.
Transactions
+----------------+----------+--------+
| transaction_id | visit_id | amount |
+----------------+----------+--------+
| 2 | 5 | 310 …
order_number | customer_number |
---|---|
1 | 1 |
2 | 2 |
3 | 3 |
4 | 3 |
-- 1위 한 명만
SELECT o.customer_number
FROM orders o
GROUP BY o.customer_number
ORDER BY COUNT …
MySQL Documentation 13.6.1 BEGIN ... END Compound Statement
[begin_label:] BEGIN
[statement_list]
END [end_label]
MySQL Documentation 13.6.5.1 CASE Statement
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
Or:
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE
Input:
Users table:
+---------+-------+
| user_id | name |
+---------+-------+
| 1 | aLice |
| 2 | bOB |
+---------+-------+
user_id is the primary key for this table.
This table contains the ID and the name of the user. The name consists of only lowercase and uppercase characters.
Output:
+---------+-------+
| user_id | name |
+---------+-------+
| 1 | Alice |
| 2 | Bob …
MySQL Documentation 13.6.6 Cursors
MySQL Documentation 13.6.7.1 DECLARE ... CONDITION Statement