简体   繁体   English

我如何从其他表中获得具有“相同”名称的所有值的客户

[英]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.

相关问题 如何提取自特定日期以来进行过交易的客户? (活跃客户定义) - How can I extract customers who have transacted since a specific date? (active customers definition) 我如何从其他用数组值替换的数据库表中获取值? - How can i get value from other db table who releted with array value? MySQL获取所有其他行如何从同一个表具有相同的ID - mysql get all other rows how have same id from same table 查询帮助 - 如何获取在前 20 家商店中的任何一家购物过的所有客户? - Query Help - How to get all customers who have shopped at any of the top 20 stores? 我需要找出从所有 4 个经销商处购买了 2 辆汽车的客户的姓名和客户 ID - I need to find out the name and customer id of customers who bought 2 cars from all the 4 dealerships 如何在我的表中指定我不能使用唯一或其他命令在 3 列中具有两个相同的值? - How Can I specify in my Table that I cannot have two same values in 3 columns with unique or other commands? 如何使用“INSERT (name, ...) INTO table SELECT name, ... FROM table_2”只插入没有相同'NAME'的行? - How to use “INSERT (name, …) INTO table SELECT name, … FROM table_2” to only insert rows who don't have the same 'NAME'? 如何返回在第二个表中有 null 的客户和其他信息 - How to return customers and additional info who have null in second table 如何从MySQL表中获取多列具有相同值的所有行? - How to get all the rows from MySQL table which have same values for multiple columns? 如何更新表,但需要从同一表的另一个选择和另一个表中获取更新值? - how can I update a table but needing to get the update value from another select of the same table and other one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM