473,513 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How best to construct this query

Claus Mygind
571 Contributor
I wish to construct a query using 4 tables.
time2 t
job j
jbook b
master m

the last table master is linked to both job and jbook so it can provide a client name for a specific job

both job and jbook are linked to a employee's time record.

I want to return from either the job or jbook tables the project name and city.

I also need the client name from the master table that is linked to either the job or jbook table.

For most of the time the information will come from the job file. Without getting into to much detail, there are times that the job does not yet exist in the table so the information has to be retrieved from the jbook table (as this is unverified information it is only to be used when not found in the job table).

I am pretty sure I want some sort of "on" clause in my select statement, where "on j.jobid is null" then use b. info...

This is what I have constructed, but I think it can be written better. So any help is appreciated:

Expand|Select|Wrap|Line Numbers
  1. select t.jobid, t.prjctname, j.prjctcity, m.name, t.workday,
  2. , concat(b.prjctcity) as bCity, concat(m2.name) as bName 
  3. from `time2` t 
  4. left join `job` j on j.jobid=t.jobid 
  5. left join `master` m on m.id=j.id 
  6. left join `jbook` b on b.jobid=t.jobid 
  7. left join `master` m2 on m2.id=b.id 
  8. where t.empNo = "999" 
  9.    and substr(t.jobid,1,1) between "0" and "9"
  10. order by t.workday desc limit 50
  11.  
Jun 1 '09 #1
3 1667
code green
1,726 Recognized Expert Top Contributor
Looks OK at first glance.
You could use USING() instead of ON to simplify the syntax.

Joining to the same table twice is usually done when
the data is required under two different conditions.
You are joining because it links to two tables which doesn't seem neccessary because an ON statement can link to more than one table
Jun 2 '09 #2
prabirchoudhury
162 New Member
hey
i dont know why you are using concat() function when you are not concatenating strings at all..


MySQL CONCAT Function

Jun 7 '09 #3
Claus Mygind
571 Contributor
@prabirchoudhury
Because I have similar name fields in both tables. Note the following:

j.prjctcity --- concat(b.prjctcity) as bCity

m.name --- concat(m2.name) as bName

referencing either prjctcity or name would generate and error. So you have to be more specific.
Jun 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2309
by: Colleyville Alan | last post by:
I have some data in a table structured like this: Date Cust_ID CUSIP Amount 01/31/2005 060208 02507M303 27,061.84...
5
4179
by: WebMatrix | last post by:
Hello, I am developing ASP.NET 1.1 application against SQL Server 2000 db, where users have ability to construct their own select criteria (for a where clause). I already developed a user...
0
1368
by: Tom | last post by:
I'm trying to adapt a function I'd written to insert arrays in MySQL to the ADOdb_lite class. The best way to do this would probably be to extend the ADOdb_lite class, but there doesn't seem to be...
6
2524
by: a | last post by:
What is the best way to construct an email in python and also attach a html file the html file to be attached is not on disk, but should be dynamically constructed in the python script I want...
2
13433
by: juan.gautier | last post by:
Hi, I try to construct a SQL code for a view to select a specific data from a table, this query take the value of the filter from a text box in a visual basic 6.0 form. my problem is when i...
41
2826
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
5
1528
by: tvmadarsh | last post by:
Hai Pls help me to write a query I am having a table with name stock
7
1506
by: rz2026 | last post by:
Given a following table with two columns date value ------------------------ 01/01/2007 0 01/02/2007 1 01/05/2007 1 ...
14
2016
by: Patrick A | last post by:
All, I have an Access DB. On a nightly basis, I want to look at an Other DB (not Access, but SQL) and: + Add any new records from Other.Clients into Access.Clients Is this something I...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7545
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7111
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.