[英]SQL subqueries to get info from a diff table
So basically, im new to SQL doing practice tutorials to better expand my knowledge. 因此,从根本上讲,我是SQL的新手入门教程,以更好地扩展我的知识。 But i im stuck on this few problems. 但是我坚持这几个问题。
How would i write and SQL query to 我将如何编写和进行SQL查询
Get the cities of agents booking an order for a customer whose cid is 'c006'. 获取代理商城市,其客户的cid为'c006'。
& &
Get the cids and names of customers who did not place an order through agent a03. 通过代理商a03获取未下订单的客户的cid和名称。
WITHOUT USING THE "JOIN" method. 无需使用“ JOIN”方法。
Database snapshot: http://i.stack.imgur.com/XicJh.png 数据库快照: http : //i.stack.imgur.com/XicJh.png
You can try this mate: 您可以尝试此伴侣:
-- Q1
SELECT city FROM agents
WHERE aid IN (
SELECT aid FROM orders
WHERE cid = 'c006'
);
-- Q2
SELECT cid, name FROM customers
WHERE cid NOT IN (
SELECT cid FROM orders
WHERE aid = 'a03'
);
Mainly you need to use join queries, your first query: 主要是您需要使用联接查询,您的第一个查询:
select a.city from Orders o
inner join Agents a on a.aid = o.aid
where o.cid = 'c006'
The second one is similar, using the other tables and fields. 第二个类似,使用其他表和字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.