Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SELECT and one to many tables
Message
 
 
À
09/07/2004 16:54:04
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00922742
Message ID:
00922792
Vues:
19
Roy,

Try
SELECT cl.*,st.cStatus,pr.cProfile, "Yes" ;
	FROM Clients cl ;
    LEFT JOIN status st ;
        ON cl.cStatus_id = st.cStatus_id ;
    LEFT JOIN profile pr ;
        ON cl.cProfile_id = pr.cProfile_id ;
	WHERE cl.iclient_id = 6305 ;
		AND EXISTS ( ;
			SELECT * FROM orders WHERE iclient_id = cl.iclient_id) ;
UNION ALL ;
SELECT cl.*,st.cStatus,pr.cProfile, "No " ;
	FROM Clients cl ;
    LEFT JOIN status st ;
        ON cl.cStatus_id = st.cStatus_id ;
    LEFT JOIN profile pr ;
        ON cl.cProfile_id = pr.cProfile_id ;
	WHERE cl.iclient_id = 6305 ;
		AND NOT EXISTS ( ;
			SELECT * FROM orders WHERE iclient_id = cl.iclient_id)
>
>I'm querying a table (clients), the query is supposed to give back one record which contains the client info and other information from other tables that are one to one tables.
>
>My problem is that one of the tables that I'm querying is one to many, so I just basically want to know if a record exists in that other table and include a field with a Yes or No, or count of how many were there, basically anything that will tell me whether the client has records in that table. If I do a join I'm going to get multiple records which I don't want. I know I can do this with multiple queries and also with the group by in VFP 6.0 but not in VFP 8 b/c of the the stricter guidelines for SQL.
>
>Let's assume the table that has the "many" is called "orders", the example below is more or less what I'm doing, but it's missing the field that will tell me whether the client has any orders. I just need to add that field.
>SELECT a.*,b.cStatus,c.cProfile ;
>FROM Clients a ;
>    LEFT JOIN status b ;
>        ON a.cStatus_id = b.cStatus_id ;
>    LEFT JOIN profile c ;
>        ON a.cProfile_id = c.cProfile_id ;
>WHERE a.iclient_id = 6305
>How do I go about doing this in just one query?
>
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform