当前位置:网站首页>[introduction to SQL in 10 days] day5+6 consolidated table
[introduction to SQL in 10 days] day5+6 consolidated table
2022-06-28 08:25:00 【Ly methane】
175. Combine two tables
surface : Person
+-------------+---------+
| Name | type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId Is the primary key column of the table .
This table contains some people's ID And their last and first names .
+-------------+---------+
| Name | type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId Is the primary key column of the table . Each row of the table contains a ID = PersonId Information about people's cities and states .
Write a SQL Query to report Person The last name of each person in the list 、 name 、 Cities and states . If personId Your address is not in Address In the table , The report is empty null .
Input :
Person surface :
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address surface :
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
Output :
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
Analysis of the answer
Left connection , personId Same connection in one piece
SELECT A.firstName, A.lastName, B.city, B.state
FROM person A LEFT JOIN address B ON A.personId = B.personId
1581. Customers who enter the store without trading
surface :Visits
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| visit_id | int |
| customer_id | int |
+-------------+---------+
visit_id Is the primary key of the table . This table contains information about customers who have visited the shopping center .
surface :Transactions
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| transaction_id | int |
| visit_id | int |
| amount | int |
+----------------+---------+
transaction_id Is the primary key of this table . This table contains visit_id Information on transactions conducted during .
Some customers may visit the shopping center but do not trade . Please write a SQL Inquire about , To find these customers ID , And the number of times they only patronize and don't trade .
Return to In any order Sorted result table .
+-------------+----------------+
| customer_id | count_no_trans |
+-------------+----------------+
| 54 | 2 |
| 30 | 1 |
| 96 | 1 |
+-------------+----------------+
Analysis of the answer
First, find out through the joint statement that there has been no transaction visit,
Then, these query results are processed according to customer_id grouping , Statistics customer_id And the number of each group
SELECT customer_id, count(visit_id) count_no_trans
FROM (
SELECT distinct A.visit_id, A.customer_id
FROM visits A LEFT JOIN transactions B ON A.visit_id = B.visit_id
WHERE B.amount IS NULL) C
GROUP BY C.customer_id
You can also check directly visits Not in transactions The data in , Then calculate in groups customer_id The same number
SELECT customer_id, count(visit_id) count_no_trans
FROM visits
WHERE visit_id NOT IN (SELECT distinct visit_id FROM transactions)
GROUP BY customer_id```
1148. Article browsing I
Views surface :
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| article_id | int |
| author_id | int |
| viewer_id | int |
| view_date | date |
+---------------+---------+
This table has no primary key , Therefore, there may be duplicate lines .
Each row of this table indicates that someone browsed an article by an author on a certain day .
Please note that , Of the same person author_id and viewer_id It's the same .
Please write one SQL Query to find all authors who have browsed their own articles , Results according to id Ascending order .
Views surface :
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date |
+------------+-----------+-----------+------------+
| 1 | 3 | 5 | 2019-08-01 |
| 1 | 3 | 6 | 2019-08-02 |
| 2 | 7 | 7 | 2019-08-01 |
| 2 | 7 | 6 | 2019-08-02 |
| 4 | 7 | 1 | 2019-07-22 |
| 3 | 4 | 4 | 2019-07-21 |
| 3 | 4 | 4 | 2019-07-21 |
+------------+-----------+-----------+------------+
Result sheet :
+------+
| id |
+------+
| 4 |
| 7 |
+------+
Analysis of the answer
Inquire about author_id and viewer_id equal
SELECT distinct author_id id
FROM views
WHERE author_id = viewer_id
ORDER BY id
197. The rising temperature
surface : Weather
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id It's the primary key of this table , The table contains temperature information for a specific date
Write a SQL Inquire about , To find out ( Yesterday's ) Of all dates with a higher temperature id .
Return results Order is not required .
Input :
Weather surface :
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+----+------------+-------------+
Output :
+----+
| id |
+----+
| 2 |
| 4 |
+----+
explain :
2015-01-02 The temperature is higher than the day before (10 -> 25)
2015-01-04 The temperature is higher than the day before (20 -> 30)
Analysis of the answer
DATEDIFF stay MySQL In the middle of the day
DATEDIFF(date1,date2) # return date1 - date2 Days of difference
Self join , The day after the query and the temperature is high
SELECT A.id
FROM weather A, Weather B
WHERE A.Temperature > B.Temperature AND DATEDIFF(A.recordDate, B.recordDate) = 1
607. Salesperson
surface : SalesPerson
+-----------------+---------+
| Column Name | Type |
+-----------------+---------+
| sales_id | int |
| name | varchar |
| salary | int |
| commission_rate | int |
| hire_date | date |
+-----------------+---------+
sales_id Is the primary key column of the table . Each row of the table shows the salesperson's name and ID , And their wages 、 Commission rate and date of employment .
surface : Company
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| com_id | int |
| name | varchar |
| city | varchar |
+-------------+---------+
com_id Is the primary key column of the table . Each row of the table indicates the name of the company and ID , And the city where the company is located .
surface : Orders
+-------------+------+
| Column Name | Type |
+-------------+------+
| order_id | int |
| order_date | date |
| com_id | int |
| sales_id | int |
| amount | int |
+-------------+------+
order_id Is the primary key column of the table .
com_id yes Company In the table com_id The foreign key .
sales_id It's from the salesperson table sales_id The foreign key .
Each row of the table contains the information of an order . This includes the company's ID 、 Salesperson's ID 、 Date of order and amount paid .
Write a SQL Inquire about , The report has nothing to do with the name “RED” The names of all salespeople for orders related to the company .
Input :
SalesPerson surface :
+----------+------+--------+-----------------+------------+
| sales_id | name | salary | commission_rate | hire_date |
+----------+------+--------+-----------------+------------+
| 1 | John | 100000 | 6 | 4/1/2006 |
| 2 | Amy | 12000 | 5 | 5/1/2010 |
| 3 | Mark | 65000 | 12 | 12/25/2008 |
| 4 | Pam | 25000 | 25 | 1/1/2005 |
| 5 | Alex | 5000 | 10 | 2/3/2007 |
+----------+------+--------+-----------------+------------+
Company surface :
+--------+--------+----------+
| com_id | name | city |
+--------+--------+----------+
| 1 | RED | Boston |
| 2 | ORANGE | New York |
| 3 | YELLOW | Boston |
| 4 | GREEN | Austin |
+--------+--------+----------+
Orders surface :
+----------+------------+--------+----------+--------+
| order_id | order_date | com_id | sales_id | amount |
+----------+------------+--------+----------+--------+
| 1 | 1/1/2014 | 3 | 4 | 10000 |
| 2 | 2/1/2014 | 4 | 5 | 5000 |
| 3 | 3/1/2014 | 1 | 1 | 50000 |
| 4 | 4/1/2014 | 1 | 4 | 25000 |
+----------+------------+--------+----------+--------+
Output :
+------+
| name |
+------+
| Amy |
| Mark |
| Alex |
+------+
According to the table orders Order in '3' and '4' , It's easy to see that there is only 'John' and 'Pam' Two salesmen once told the company 'RED' Sold .
So we need output meters salesperson The names of all the others in .
Analysis of the answer
First in Company Find the company in the table RED The company ID
And then Orders Find out in the table that the company's order involves sales_id
Last query SalesPerson In the table Query the names of people who are not in the involved list
SELECT A.name
FROM salesperson A
WHERE A.sales_id NOT IN(SELECT C.sales_id as id
FROM orders C
WHERE C.com_id IN(SELECT B.com_id
FROM Company B
WHERE B.name = 'RED'))
summary
- League table query , Left connection
- GROUP BY and count
- DATEDIFF(date1,date2) # return date1 - date2 Days of difference
- Classic subquery
边栏推荐
猜你喜欢

你了解TCP協議嗎(二)?

Why MySQL cannot insert Chinese data in CMD

Do you know TCP protocol (1)?

Unity gets the coordinate point in front of the current object at a certain angle and distance

Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)

887. egg drop

Kubernetes notes and the latest k3s installation introduction

Kali Notes(1)

WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持

叠加阶梯图和线图及合并线图和针状图
随机推荐
Jenkins' common build trigger and hook services (V)
CloudCompare&PCL 点云SVD分解
抖音服务器带宽有多大,才能供上亿人同时刷?
B_ QuRT_ User_ Guide(28)
为什么函数模板没有偏特化?
Kali Notes(1)
[learning notes] matroid
[learning notes] differential constraint
AI首席架构师8-AICA-高翔 《深入理解和实践飞桨2.0》
A - Bi-shoe and Phi-shoe
Force buckle 1884 Egg drop - two eggs
设置cmd的编码为utf-8
yaml json
nuxt3入门
ROS 笔记(09)— 参数的查询和设置
AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
B_QuRT_User_Guide(26)
Eslint 语法监测关闭
Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19
[learning notes] linear basis