簡體   English   中英

SQL 查詢返回零行

[英]SQL query returns zero rows

我必須為每個用戶查找加入日期以及他們作為買家在 2019 年下的訂單數量。

Table: Users
+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| user_id        | int     |
| join_date      | date    |
| favorite_brand | varchar |
+----------------+---------+

user_id 是該表的主鍵。 此表包含在線購物網站的用戶信息,用戶可以在該網站上銷售和購買商品。

表:訂單

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| order_date    | date    |
| item_id       | int     |
| buyer_id      | int     |
| seller_id     | int     |
+---------------+---------+

order_id 是該表的主鍵。 item_id 是 Items 表的外鍵。 買家 ID 和賣家 ID 是用戶表的外鍵。

表:項目

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| item_id       | int     |
| item_brand    | varchar |
+---------------+---------+
item_id is the primary key of this table.

輸入:

用戶表:

+---------+------------+----------------+
| user_id | join_date  | favorite_brand |
+---------+------------+----------------+
| 1       | 2018-01-01 | Lenovo         |
| 2       | 2018-02-09 | Samsung        |
| 3       | 2018-01-19 | LG             |
| 4       | 2018-05-21 | HP             |
+---------+------------+----------------+

訂單表:

+----------+------------+---------+----------+-----------+
| order_id | order_date | item_id | buyer_id | seller_id |
+----------+------------+---------+----------+-----------+
| 1        | 2019-08-01 | 4       | 1        | 2         |
| 2        | 2018-08-02 | 2       | 1        | 3         |
| 3        | 2019-08-03 | 3       | 2        | 3         |
| 4        | 2018-08-04 | 1       | 4        | 2         |
| 5        | 2018-08-04 | 1       | 3        | 4         |
| 6        | 2019-08-05 | 2       | 2        | 4         |
+----------+------------+---------+----------+-----------+

物品表:

+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1       | Samsung    |
| 2       | Lenovo     |
| 3       | LG         |
| 4       | HP         |
+---------+------------+

輸出:

+-----------+------------+----------------+
| buyer_id  | join_date  | orders_in_2019 |
+-----------+------------+----------------+
| 1         | 2018-01-01 | 1              |
| 2         | 2018-02-09 | 2              |
| 3         | 2018-01-19 | 0              |
| 4         | 2018-05-21 | 0              |
+-----------+------------+----------------+

我寫了以下解決方案,但它不起作用。 請解釋它有什么問題。

select u.user_id as buyer_id, u.join_date as join_date, count(o.order_id) as orders_in_2019
from Users u join Orders o on u.user_id = o.buyer_id
where o.order_date < date('2010-01-01') and o.order_date >= date('2019-01-01')
group by u.user_id;

這給了我空的結果。

除了日期錯字(o.order_date < '2010-01-01')之外,您似乎希望所有用戶即使他們在 2019 年沒有訂單,所以左連接和條件聚合似乎是合適的

select u.user_id as buyer_id, u.join_date as join_date
       ,sum(case when o.order_date < '2020-01-01' and o.order_date >= '2019-01-01' then 1 else 0 end) cnt
from users u 
left join Orders o on u.user_id = o.buyer_id
group by u.user_id ;

+----------+------------+------+
| buyer_id | join_date  | cnt  |
+----------+------------+------+
|        1 | 2018-01-01 |    1 |
|        2 | 2018-02-09 |    2 |
|        3 | 2018-01-19 |    0 |
|        4 | 2018-05-21 |    0 |
+----------+------------+------+
4 rows in set (0.001 sec)

您無法通過關閉選擇不在組中的直接元素

select g_user.buyer_id, Users.join_date as join_date , g_user.orders_in_2019 From (select u.user_id as buyer_id, count(o.order_id) as orders_in_2019 From Users u 
join Orders o on u.user_id = o.buyer_id
where o.order_date < date('2010-01-01') and o.order_date >= date('2019-01-01')
group by u.user_id) 
as g_user join Users on g_user.buyer_id = Users.user_id;

這是因為您的日期在 sql 中不正確:

o.order_date < date('2010-01-01') 

很明顯,您的訂單都不是在 2010 年之前。您應該更改為

o.order_date < date('2020-01-01') 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM