[英]SQL ACCESS (need ideas and help about a query)
I am new at access SQL and i need help with some query. 我是Access SQL的新手,我需要一些查询的帮助。 What i want is to find those customers that prefer a car (manufacturer and model) from
prefer_to_buy
AND prefer_to_rent
that no one else prefer. 我想要的是从
prefer_to_buy
和prefer_to_rent
中找到那些更喜欢汽车(制造商和模型)的客户,而其他人都没有。
For example if 2 customers prefer toyota aygo must not be in the result table. 例如,如果2位顾客更喜欢Toyota Aygo,则必须不在结果表中。
customer(customer_id,name)
客户(customer_id,姓名)
prefer_to_buy(customer_id,manufacturer,model)
preferred_to_buy(客户ID,制造商,型号)
prefer_to_rent(customer_id, manufacturer,model)
preferred_to_rent(客户ID,制造商,型号)
I have tried a lot of ways including exists and i know there must be about 2-3 subqueries but i cant get it to work, any ideas? 我尝试了很多方式,包括存在,我知道大约必须有2-3个子查询,但是我无法使其正常工作,有什么想法吗?
Your problem definition is very vague, so the answer is also sort of generic. 您的问题定义非常模糊,因此答案也很笼统。 You should try to create a Left Outer Join on
customer
table and, for example, a " prefer_to_buy
" Table using customer_id
as a join field, and include: customer_id,name
from the left table and manufacturer,model
from the right table. 您应该尝试在
customer
表上创建一个左外部联接,例如,使用customer_id
作为prefer_to_buy
字段来创建一个“ prefer_to_buy
”表,并包括: customer_id,name
左表中的customer_id,name
, manufacturer,model
右表中的manufacturer,model
。 The same logic applies to prefer_to_rent
Table: you can actually combine these 3 Tables in a single Access SQL query using the aforementioned Outer Joins. 相同的逻辑适用于
prefer_to_rent
表:您实际上可以使用上述外部联接将这3个表组合到单个Access SQL查询中。
Hope this may help. 希望这会有所帮助。 Best regards,enter code here
最好的问候,在这里输入代码
There are a couple of parts to cover here. 这里有两个部分。 First, you could use the
union
operator to treat prefer_to_buy
and prefer_to_rent
as a single table (possibly with an additional literal "column" to indicate preference type). 首先,您可以使用
union
运算符将prefer_to_buy
和prefer_to_rent
视为单个表(可能带有附加的文字“列”以指示首选项类型)。 Once you've done this, you could use the exists
operator to make sure no other customers prefer this car: 完成此操作后,可以使用
exists
运算符来确保没有其他顾客喜欢这辆车:
SELECT c.name, p.manufacturer, p.model
FROM customer c
JOIN (SELECT customer_id, manufacturer, model
FROM prefer_to_buy
UNION
SELECT customer_id, manufacturer, model
FROM prefer_to_buy) p ON c.customer_id = p.customer_id
WHERE NOT EXISTS (SELECT *
FROM prefer_to_buy pb
WHERE c.customer_id != pb.customer_id AND
p.manufacturer = pb.manufacturer AND
p.model = pb.model) AND
NOT EXISTS (SELECT *
FROM prefer_to_rent pr
WHERE c.customer_id != pr.customer_id AND
p.manufacturer = pr.manufacturer AND
p.model = pr.model)
First you need to do to two joins one on prefer_to_buy AND one on prefer_to_rent. 首先,您需要对两个联接执行一个,对joint_to_buy进行一次联接,对joint_to_rent进行一个联接。 Next, you need to check if any one else would like the same manufacturer and model.
接下来,您需要检查是否还有其他人想要相同的制造商和型号。 Do this with a exist
用存在做到这一点
SELECT *
FROM customer AS c
JOIN prefer_to_buy AS pb ON c.customer_id = pb.customer_id
JOIN prefer_rent AS pr ON c.customer_id = pr.customer_id
WHERE NOT EXISTS ( SELECT 'x' FROM prefer_to_buy AS pb1 WHERE pb.manufacturer = pb1.manufacturer AND pb.model = pb1.model AND pb.customer_id <> pb1.customer_id)
AND NOT EXISTS ( SELECT 'x' FROM prefer_to_rent AS pr1 WHERE pb.manufacturer = pr1.manufacturer AND pb.model = pr1.model AND pb.customer_id <> pr1.customer_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.