[英]How can I get customers who have all values of “same” name from other table
I have problem. 我有问题。 I have tables: 我有桌子:
Customer(cid,etc..),
Motorbike(mid, name, etc..)
and table 和桌子
Rent(rid,cid, mid, check_in, check_out, etc..).
Name of motorbike includes brand (like Yamaha XT660 R, Yamaha R6, Honda CBF125R, etc.) 摩托车名称包括品牌(例如Yamaha XT660 R,Yamaha R6,Honda CBF125R等)
My task is to find customers who rented all Yamaha or Suzuki motorbikes. 我的任务是找到租用所有Yamaha或Suzuki摩托车的客户。 My query: select c.name, m.name, count(c.name) from motorbike m, rent r, customer c where c.cid = r.cid AND r.mid = m.mid and (m.name like 'Yamaha%' or m.name like 'Suzuki%') group by c.ime 我的查询:从摩托车m,租金r,客户c中选择c.name,m.name,count(c.name),其中c.cid = r.cid AND r.mid = m.mid和(m.name如' Yamaha%'或m.name(例如“ Suzuki%'))
This sounds like you should be able to solve it with a straightforward JOIN
query: 听起来您应该可以通过简单的JOIN
查询来解决它:
SELECT
r.rid, m.name, c.*
FROM
Rent r
JOIN Motorbike m
ON r.mid = m.mid
JOIN Costumer c
ON r.cid = c.cid
WHERE
m.name LIKE 'Yamaha%'
OR m.name LIKE 'Honda%'
ORDER BY
m.name, r.check_in
The above will select the rental ID, bike name and full customer information for each rental for all Yamaha / Honda bikes. 上面将为所有Yamaha / Honda自行车的每次租赁选择租赁ID,自行车名称和完整的客户信息。
If you want to get a "unique" list of customers that rented said bikes, but not the full rental history, you can leverage GROUP BY
: 如果您想获得租用所述自行车的“唯一”客户列表,而不是完整的租车历史记录,则可以利用GROUP BY
:
SELECT
c.cid, m.name, COUNT(*) AS num_rentals
FROM
Rent r
JOIN Motorbike m
ON r.mid = m.mid
JOIN Costumer c
ON r.cid = c.cid
WHERE
m.name LIKE 'Yamaha%'
OR m.name LIKE 'Honda%'
GROUP BY
c.cid
Try this: 尝试这个:
SELECT C.*,m.mid,m.name as BikeName,R.check_out,R.check_in
FROM Costumer C JOIN
Rent R ON R.cid=C.cid JOIN
Motorbike M ON R.bid=M.mid
WHERE m.name LIKE 'Yamaha%'
OR m.name LIKE 'Honda%'
You can get customers who rented at least on e Yamaha and at least on Suzuki by using group by
: 通过使用group by
您可以吸引至少在e Yamaha上和至少在Suzuki上租过的客户:
select c.*
from Rent r join
Motorbike m
on r.mid = m.mid join
Customer c
on r.cid = c.cid
group by c.id
having sum(m.name LIKE 'Yamaha%') > 0 and
sum(m.name LIKE 'Honda%') > 0;
Each condition in the having
clause counts the number of motorcycles of a given type. 中每个条件having
子句计数给定类型的摩托车的数量。 You want at least one of each. 您至少要每个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.