Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Advice needed on Sql Select
Message
From
26/04/2014 10:19:21
 
 
To
25/04/2014 17:26:33
Mike Yearwood
Toronto, Ontario, Canada
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2008 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01599095
Message ID:
01599111
Views:
60
>>>>>Here's my situation. I'd like to get info from different tables so I'll be using a join.
>>>>>
>>>>>as an example let's say I have an employee table. In that table there is the id of the employee, when the employee starts his day defined as an integer and when the employee finished his day defined once again as an integer.
>>>>>
>>>>>Now let's say we have a table containing a bunch of times.
>>>>>
>>>>>structure of the time table could be an integer for the primary key and a string for the time possible and this would be 5 characters.
>>>>>
>>>>>
>>>>>Table employee
>>>>>
>>>>>EmployeeId AutoInt
>>>>>BeginTime Integer
>>>>>EndTime Integer
>>>>>
>>>>>
>>>>>Table TimeDef
>>>>>IdTime Integer
>>>>>DescTime C5
>>>>>
>>>>>a select for this could be :
>>>>>
>>>>>select EmployeeId, DescTime ;
>>>>>from Employee ;
>>>>>left join TimeDef on employee.BeginTime = TimeDef.IdTime
>>>>>
>>>>>This previous select should work ok. But how do I get the description (DescTime) for my EndTime in the same select?
>>>>
>>>>You need to join with your timedef table twice under different alias, e.g.
>>>>
>>>>select EmployeeId, t1.DescTime  as StartTime, t2.DescTime as EndTime;
>>>>from Employee ;
>>>>left join TimeDef t1 on employee.BeginTime = t1.IdTime -- why left JOIN?
>>>>LEFT JOIN TimeDef t2 on Employee.EndTime = t2.IdTime
>>>>
>>>>Also, why you're using LEFT JOIN instead of the INNER JOIN?
>>>
>>>Thanks you Naomi. Why should I use inner instead of Left?
>>
>>Because most likely there is a relationship (it should be) between these two tables, so you should not have times not defined in the TimeDef table. And that's being the case, it should be an inner join.
>>Unless, of course, some times are NULL in the Employee table. In this case LEFT JOIN is correct.
>>
>>BTW, some database experts prefer to use plural for table names.
>
>Whether the table is plural or singular is merely personal preference. A phone book is singular. It contains nameS, addressES and phone numberS of people or persons. What affects readability far more is avoiding arbitrary aliases such as t1 and t2 and instead using more descriptive aliases such as BeginDef and EndDef
>
>
select ;
>  Employee.EmployeeId, ;
>  Employee.BeginTime, ;
>  BeginDef.DescTime  as BeginDef, ;
>  Employee.EndTime, ;
>  EndDef.DescTime as EndDef ;
>from Employee ;
>LEFT JOIN TimeDef BeginDef on Employee.BeginTime = BeginDef.IdTime
>LEFT JOIN TimeDef EndDef on Employee.EndTime = EndDef.IdTime
>
>You can now see that BeginTime has a corresponding BeginDef and EndTime has a corresponding EndDef. You can infer that BeginDef and EndDef are aliases of TimeDef and you won't be pausing to wonder if t1 or t2 was the start or end alias. Such things really have a beneficial impact in more complex queries.
>
>LEFT JOINs also show employee records with missing data or records that preceded the eventual addition of TimeDef as a new feature.

Good suggestion Mike for alias names. Thanks.
*******************************************************
Save a tree, eat a beaver.
Denis Chassé
Previous
Reply
Map
View

Click here to load this message in the networking platform