Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Query question
Message
 
À
20/03/2015 14:44:13
James Blackburn
Qualty Design Systems, Inc.
Kuna, Idaho, États-Unis
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Titre:
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MySQL
Application:
Web
Divers
Thread ID:
01617030
Message ID:
01617039
Vues:
46
>My nephew asked for some help on a query and so far I can't seem to think of a good way to do it. Here is his question.
>
>A one-to-many relationship where the parent table is a list of customers and the child table is a list of things they've bought. It's easy enough to select customers who have bought a television. It's also simple to select customers who have bought a television or a couch. But how do I select customers who have bought both a television and a couch? The following is returning customers who have bought both or one of the items:
>
>
>I don't have data to test with and I was hoping someone could had an idea.
>
>Thanks
create table #Customers (CustomerID int, CustomerName varchar(50))
create table #Products (ProductID int, ProductName varchar(50), Price Money)
create table #Sales (ProductID int, CustomerID int, Quantity int)

insert into #Customers (CustomerID, CustomerName) values (1, 'Robert'), (2, 'James'), (3, 'Anderson')
insert into #Products (ProductID, ProductName, Price) values (1, 'Apple', 1), (2, 'Orange', 5.3), (3, 'Grapes', 2)
insert into #Sales (ProductID, CustomerID, Quantity) values (1, 1, 1), (1, 2, 3), (2, 1, 2), (3, 1, 5), (2, 2, 2), (1, 1, 1)

select		distinct
		Cust.CustomerId, 
		Cust.CustomerName
	from	#Customers Cust
	inner	join #Sales Sp1 On Cust.CustomerId = Sp1.CustomerId and Sp1.ProductID = (select ProductID from #Products where ProductName = 'Orange')
	inner	join #Sales Sp2 On Cust.CustomerId = Sp2.CustomerId and Sp2.ProductID = (select ProductID from #Products where ProductName = 'Apple')

drop table #Customers
drop table #Products
drop table #Sales
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform