我正在使用sql developer和oracle 11g为酒店系统创建数据库,我需要打印first_name,last_name,电子邮件以及每个人(电子邮件)访问酒店的次数。因此,我需要计算预订表中发送电子邮件的次数。
我有2张桌子要加入:
first_name
last_name
phone
address
town
email
postcode
Date_
room
occupants
arrival_time
nights
paid
email_
我希望显示first_name, last_name, email, COUNT(email)
我该怎么做?
答案 0 :(得分:0)
SELECT first_name, last_name, customer.email, visits
FROM customer
JOIN (SELECT email_, COUNT(*) AS visits
FROM bookings
GROUP BY email_) v
ON customer.email = v.email_
答案 1 :(得分:0)
这将是一对多的关系:
SELECT c.first_name, c.last_name, c.email, COUNT(c.email)
FROM customer c
JOIN booking b
ON c.email = b.email_
GROUP BY
c.first_name, c.last_name, c.email
;
与:
相同SELECT x.col, count(x.col)
FROM
(
SELECT 1 col FROM DUAL UNION ALL
SELECT 2 FROM DUAL
) x
JOIN
(
SELECT 1 col FROM DUAL UNION ALL
SELECT 1 FROM DUAL UNION ALL
SELECT 2 FROM DUAL UNION ALL
SELECT 2 FROM DUAL UNION ALL
SELECT 2 FROM DUAL UNION ALL
SELECT 3 FROM DUAL
) y
ON x.col = y.col
GROUP BY
x.col
;
/*
1 2
2 3
*/