Re: Help with a JOIN query please
"shaun thornburgh" <[EMAIL PROTECTED]> wrote on 03/17/2005 06:46:22 PM: > Hi, > > I have ( among others ) three tables in my database: Claims, Expenses and > Mileage. A claim can contain many expense entries and many mileage entries. > I am using the follwing query to show the total expenses and mileage per > claim for a particulare user: > > SELECT C.*, SUM(E.Amount) AS Amount, SUM(M.Mileage) AS Mileage > FROM Claims C > LEFT JOIN Expenses E ON E.Claim_ID = C.Claim_ID > LEFT JOIN Mileage M ON M.Claim_ID = C.Claim_ID > WHERE C.Claimant_ID = '1' > GROUP BY C.Claim_ID > > The problem is if there are two mileage entries and one expense entry the > expense total is doubled (and vice versa), can some explain this to me > please? > > Thanks for your help. > > TABLE DEFINITIONS: > > mysql> desc Claims; > +-+-+--+-+-++ > | Field | Type| Null | Key | Default | Extra | > +-+-+--+-+-++ > | Claim_ID| int(11) | | PRI | NULL| auto_increment | > | Claimant_ID | int(11) | | | 0 | | > | Description | varchar(50) | | | | | > | Status | varchar(50) | YES | | Open| | > | Submission_Date | datetime| YES | | NULL| | > | Approval_Date | datetime| YES | | NULL| | > | Approver_ID | int(11) | YES | | NULL| | > +-+-+--+-+-++ > 7 rows in set (0.00 sec) > > mysql> desc Expenses; > +-+---+--+-+- > ++ > | Field | Type | Null | Key | Default | Extra > | > +-+---+--+-+- > ++ > | Expense_ID | int(11) | | PRI | NULL| > auto_increment | > | Claim_ID| int(11) | YES | | NULL| > | > | Description | varchar(50) | YES | | NULL| > | > | Expense_Category_ID | int(11) | YES | | NULL| > | > | Insertion_Date | date | YES | | NULL| > | > | Project_ID | int(11) | YES | | NULL| > | > | Amount | decimal(10,2) | YES | | NULL| > | > | Rate_ID | int(11) | YES | | NULL| > | > | Supplier_ID | int(11) | YES | | NULL| > | > | Receipt | varchar(10) | YES | | NULL| > | > | Receipt_Date| varchar(10) | YES | | NULL| > | > | VAT_Receipt | varchar(10) | YES | | NULL| > | > | VAT_Amount | decimal(10,2) | YES | | NULL| > | > +-+---+--+-+- > ++ > 13 rows in set (0.00 sec) > > mysql> desc Mileage; > ++--+--+-+++ > | Field | Type | Null | Key | Default| Extra | > ++--+--+-+++ > | Mileage_ID | int(11) | | PRI | NULL | auto_increment | > | Claim_ID | int(11) | | | 0 | | > | Project_ID | int(11) | | | 0 | | > | Insertion_Date | date | | | -00-00 | | > | Description| varchar(255) | | || | > | Start_Mileage | int(11) | | | 0 | | > | End_Mileage| int(11) | | | 0 | | > | Mileage| int(11) | | | 0 | | > ++--+--+-+++ > 8 rows in set (0.00 sec) > > mysql> > It's happening because you are joining two child tables at once. Maybe a sequence of diagrams will help. This is what happens within the DB engine whenever you make a JOIN. Since you were nice enough to post your table structures (THANK YOU!!!) I will use your tables and problem query as examples. Something simple: SELECT ... FROM Claims C LEFT JOIN Expenses E ON E.Claim_ID = C.Claim_ID; Step one of any query is to identify all of your source data. In this case, we are combining the records of two tables so that it behaves like a single larger table. This internally created, virtual table represents all possible combinations of rows that satisfy the ON conditions of your JOIN clauses. INTERNAL VIRTUAL TABLE A +---+-+ | all columns of Claims | all columns of Expenses | +---+-+ | Claims row 1 | all null values | +---+-+ | Claims row 2 | Expenses row 246| +---+-+ | Claims row 2 | Expenses row
Re: Help with a JOIN query please
Hello shaun, May be with subselects you can do what you want: SELECT C.*,(select SUM(E.Amount) from Expenses E where E.Claim_ID = C.Claim_ID)AS Amount, (select SUM(M.Mileage) from Mileage M where M.Claim_ID = C.Claim_ID) AS Mileage FROM Claims C WHERE C.Claimant_ID = '1' st> Hi, st> I have ( among others ) three tables in my database: Claims, Expenses and st> Mileage. A claim can contain many expense entries and many mileage entries. st> I am using the follwing query to show the total expenses and mileage per st> claim for a particulare user: st> SELECT C.*, SUM(E.Amount) AS Amount, SUM(M.Mileage) AS Mileage st> FROM Claims C st> LEFT JOIN Expenses E ON E.Claim_ID = C.Claim_ID st> LEFT JOIN Mileage M ON M.Claim_ID = C.Claim_ID st> WHERE C.Claimant_ID = '1' st> GROUP BY C.Claim_ID st> The problem is if there are two mileage entries and one expense entry the st> expense total is doubled (and vice versa), can some explain this to me st> please? st> Thanks for your help. st> TABLE DEFINITIONS: mysql>> desc Claims; st> +-+-+--+-+-++ st> | Field | Type| Null | Key | Default | Extra | st> +-+-+--+-+-++ st> | Claim_ID| int(11) | | PRI | NULL| auto_increment | st> | Claimant_ID | int(11) | | | 0 | st> | Description | varchar(50) | | | | st> | Status | varchar(50) | YES | | Open| st> | Submission_Date | datetime| YES | | NULL| st> | Approval_Date | datetime| YES | | NULL| st> | Approver_ID | int(11) | YES | | NULL| st> +-+-+--+-+-++ st> 7 rows in set (0.00 sec) mysql>> desc Expenses; st> +-+---+--+-+-++ st> | Field | Type | Null | Key | Default | Extra st> | st> +-+---+--+-+-++ st> | Expense_ID | int(11) | | PRI | NULL| st> auto_increment | st> | Claim_ID| int(11) | YES | | NULL| st> | st> | Description | varchar(50) | YES | | NULL| st> | st> | Expense_Category_ID | int(11) | YES | | NULL| st> | st> | Insertion_Date | date | YES | | NULL| st> | st> | Project_ID | int(11) | YES | | NULL| st> | st> | Amount | decimal(10,2) | YES | | NULL| st> | st> | Rate_ID | int(11) | YES | | NULL| st> | st> | Supplier_ID | int(11) | YES | | NULL| st> | st> | Receipt | varchar(10) | YES | | NULL| st> | st> | Receipt_Date| varchar(10) | YES | | NULL| st> | st> | VAT_Receipt | varchar(10) | YES | | NULL| st> | st> | VAT_Amount | decimal(10,2) | YES | | NULL| st> | st> +-+---+--+-+-++ st> 13 rows in set (0.00 sec) mysql>> desc Mileage; st> ++--+--+-+++ st> | Field | Type | Null | Key | Default| Extra | st> ++--+--+-+++ st> | Mileage_ID | int(11) | | PRI | NULL | auto_increment | st> | Claim_ID | int(11) | | | 0 | st> | Project_ID | int(11) | | | 0 | st> | Insertion_Date | date | | | -00-00 | st> | Description| varchar(255) | | || st> | Start_Mileage | int(11) | | | 0 | st> | End_Mileage| int(11) | | | 0 | st> | Mileage| int(11) | | | 0 | st> ++--+--+-+++ st> 8 rows in set (0.00 sec) mysql>> -- Best regards, Krasimir_Slaveykov, 18 Ìàðò 2005 ã., 12:54:56 mailto: [EMAIL PROTECTED] [EMAIL PROTECTED] |-| |/ * * *** * ** /| | *** *** *** *** *** *** ** *** //| |/// *** *** * * *** **** *** ///| |// *** *** *** *** *** *** ** ****** | |/ *** *** *** * *** /| |--- www.office1.bg --| -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Help with a JOIN query please
Hi, I have ( among others ) three tables in my database: Claims, Expenses and Mileage. A claim can contain many expense entries and many mileage entries. I am using the follwing query to show the total expenses and mileage per claim for a particulare user: SELECT C.*, SUM(E.Amount) AS Amount, SUM(M.Mileage) AS Mileage FROM Claims C LEFT JOIN Expenses E ON E.Claim_ID = C.Claim_ID LEFT JOIN Mileage M ON M.Claim_ID = C.Claim_ID WHERE C.Claimant_ID = '1' GROUP BY C.Claim_ID The problem is if there are two mileage entries and one expense entry the expense total is doubled (and vice versa), can some explain this to me please? Thanks for your help. TABLE DEFINITIONS: mysql> desc Claims; +-+-+--+-+-++ | Field | Type| Null | Key | Default | Extra | +-+-+--+-+-++ | Claim_ID| int(11) | | PRI | NULL| auto_increment | | Claimant_ID | int(11) | | | 0 || | Description | varchar(50) | | | || | Status | varchar(50) | YES | | Open|| | Submission_Date | datetime| YES | | NULL|| | Approval_Date | datetime| YES | | NULL|| | Approver_ID | int(11) | YES | | NULL|| +-+-+--+-+-++ 7 rows in set (0.00 sec) mysql> desc Expenses; +-+---+--+-+-++ | Field | Type | Null | Key | Default | Extra | +-+---+--+-+-++ | Expense_ID | int(11) | | PRI | NULL| auto_increment | | Claim_ID| int(11) | YES | | NULL| | | Description | varchar(50) | YES | | NULL| | | Expense_Category_ID | int(11) | YES | | NULL| | | Insertion_Date | date | YES | | NULL| | | Project_ID | int(11) | YES | | NULL| | | Amount | decimal(10,2) | YES | | NULL| | | Rate_ID | int(11) | YES | | NULL| | | Supplier_ID | int(11) | YES | | NULL| | | Receipt | varchar(10) | YES | | NULL| | | Receipt_Date| varchar(10) | YES | | NULL| | | VAT_Receipt | varchar(10) | YES | | NULL| | | VAT_Amount | decimal(10,2) | YES | | NULL| | +-+---+--+-+-++ 13 rows in set (0.00 sec) mysql> desc Mileage; ++--+--+-+++ | Field | Type | Null | Key | Default| Extra | ++--+--+-+++ | Mileage_ID | int(11) | | PRI | NULL | auto_increment | | Claim_ID | int(11) | | | 0 || | Project_ID | int(11) | | | 0 || | Insertion_Date | date | | | -00-00 || | Description| varchar(255) | | ||| | Start_Mileage | int(11) | | | 0 || | End_Mileage| int(11) | | | 0 || | Mileage| int(11) | | | 0 || ++--+--+-+++ 8 rows in set (0.00 sec) mysql> -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Help with a join query please!
Hi Shawn, Sorry for the confusions! The booking in question is where B.Booking_ID = x as in the original query: SELECT U.User_ID, U.User_Firstname, U.User_Lastname FROM Allocations A, Users U LEFT JOIN Bookings B ON B.User_ID = U.User_ID AND B.Booking_ID = 4512 WHERE U.User_ID = A.User_ID AND A.Project_ID = 11 ORDER BY User_Firstname; Thanks, Shaun From: [EMAIL PROTECTED] To: "shaun thornburgh" <[EMAIL PROTECTED]> CC: mysql@lists.mysql.com,[EMAIL PROTECTED] Subject: Re: Help with a join query please! Date: Wed, 29 Dec 2004 15:58:43 -0500 "shaun thornburgh" <[EMAIL PROTECTED]> wrote on 12/29/2004 03:00:08 PM: > Hi Shawn, > > Thanks for your reply, but your query seems to return everyone allocated to > that project plus everyone who has ever been booked on that project. What I > need is everyone who is currently allocated to that project and the staff > member who has been booked for the booking in question whether they have > been allocated to the project or not. How can I determine the "booking in question"? What confluence of values makes a "generic" booking the "booking in question". If you could walk me through the process of finding it manually ("compare field x on table y to field z on table bb then go over to table c and look at ...") I can get you the data you need. > > In reply to your quesion bookings don't relate to allocations directly. A > booking relates to a project by Project_ID and to a user by User_ID. > Allocations contains both of these fields to determine who is 'Allocated' to > a particular project. > > Thanks again for your help! > any time :-) Shawn Green Database Administrator Unimin Corporation - Spruce Pine > > >From: [EMAIL PROTECTED] > >To: "shaun thornburgh" <[EMAIL PROTECTED]> > >CC: mysql@lists.mysql.com,[EMAIL PROTECTED] > >Subject: Re: Help with a join query please! > >Date: Thu, 23 Dec 2004 10:14:24 -0500 > > > >So -- what's the field that relates a booking to an allocation? Do they > >share a project_ID or what? If they do, you might try this: > > > >SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname > >FROM Users U > >LEFT JOIN Allocations A > > on A.User_ID = U.User_ID > >LEFT JOIN Bookings B > > ON B.User_ID = U.User_ID > >WHERE A.Project_ID = '11' > >OR B.Project_ID = '11' > >ORDER BY User_Firstname; > > > > > >A pet peeve of mine is when people 'quote' NUMBERS. According to the > >extremely well written manual, you only need to quote STRING values and > >DATETIME values. Unless the columns Project_ID and Booking_ID are some > >form of STRING column (CHAR, VARCHAR, TEXT, etc.) you don't need to quote > >their values in queries. It forces the query engine to perform an > >unnecessary internal type conversion. Here is what I think your query > >should look like: > > > >SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname > >FROM Users U > >LEFT JOIN Allocations A > > on A.User_ID = U.User_ID > >LEFT JOIN Bookings B > > ON B.User_ID = U.User_ID > >WHERE A.Project_ID = 11 > >OR B.Project_ID = 11 > >ORDER BY User_Firstname; > > > > > >I used SELECT DISTINCT so that in the event that someone was both BOOKED > >and ALLOCATED to the same project, you only got them listed once. > > > >Shawn Green > >Database Administrator > >Unimin Corporation - Spruce Pine > > > >"shaun thornburgh" <[EMAIL PROTECTED]> wrote on 12/23/2004 > >08:37:37 AM: > > > > > Hi, > > > > > > Thanks for your reply but that produces exactly the same result... > > > > > > Any ideas? > > > > > > >From: Sasha Pachev <[EMAIL PROTECTED]> > > > >To: shaun thornburgh <[EMAIL PROTECTED]> > > > >CC: mysql@lists.mysql.com > > > >Subject: Re: Help with a join query please! > > > >Date: Tue, 21 Dec 2004 14:57:43 -0700 > > > > > > > >shaun thornburgh wrote: > > > >>Hi, > > > >> > > > >>I have (among others) three tables in my database that i am struggling > > > > > >>with a join query on. The database holds dates for Bookings. If Users > >are > > > >>Allocated to a particular Project they can be booked. However if a > >user is > > > >>booked but then unallocated I want to be able to display all peolple > > > >>allocated to that project plus the person originally booked. Here are &
Re: Help with a join query please!
"shaun thornburgh" <[EMAIL PROTECTED]> wrote on 12/29/2004 03:00:08 PM: > Hi Shawn, > > Thanks for your reply, but your query seems to return everyone allocated to > that project plus everyone who has ever been booked on that project. What I > need is everyone who is currently allocated to that project and the staff > member who has been booked for the booking in question whether they have > been allocated to the project or not. How can I determine the "booking in question"? What confluence of values makes a "generic" booking the "booking in question". If you could walk me through the process of finding it manually ("compare field x on table y to field z on table bb then go over to table c and look at ...") I can get you the data you need. > > In reply to your quesion bookings don't relate to allocations directly. A > booking relates to a project by Project_ID and to a user by User_ID. > Allocations contains both of these fields to determine who is 'Allocated' to > a particular project. > > Thanks again for your help! > any time :-) Shawn Green Database Administrator Unimin Corporation - Spruce Pine > > >From: [EMAIL PROTECTED] > >To: "shaun thornburgh" <[EMAIL PROTECTED]> > >CC: mysql@lists.mysql.com,[EMAIL PROTECTED] > >Subject: Re: Help with a join query please! > >Date: Thu, 23 Dec 2004 10:14:24 -0500 > > > >So -- what's the field that relates a booking to an allocation? Do they > >share a project_ID or what? If they do, you might try this: > > > >SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname > >FROM Users U > >LEFT JOIN Allocations A > > on A.User_ID = U.User_ID > >LEFT JOIN Bookings B > > ON B.User_ID = U.User_ID > >WHERE A.Project_ID = '11' > >OR B.Project_ID = '11' > >ORDER BY User_Firstname; > > > > > >A pet peeve of mine is when people 'quote' NUMBERS. According to the > >extremely well written manual, you only need to quote STRING values and > >DATETIME values. Unless the columns Project_ID and Booking_ID are some > >form of STRING column (CHAR, VARCHAR, TEXT, etc.) you don't need to quote > >their values in queries. It forces the query engine to perform an > >unnecessary internal type conversion. Here is what I think your query > >should look like: > > > >SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname > >FROM Users U > >LEFT JOIN Allocations A > > on A.User_ID = U.User_ID > >LEFT JOIN Bookings B > > ON B.User_ID = U.User_ID > >WHERE A.Project_ID = 11 > >OR B.Project_ID = 11 > >ORDER BY User_Firstname; > > > > > >I used SELECT DISTINCT so that in the event that someone was both BOOKED > >and ALLOCATED to the same project, you only got them listed once. > > > >Shawn Green > >Database Administrator > >Unimin Corporation - Spruce Pine > > > >"shaun thornburgh" <[EMAIL PROTECTED]> wrote on 12/23/2004 > >08:37:37 AM: > > > > > Hi, > > > > > > Thanks for your reply but that produces exactly the same result... > > > > > > Any ideas? > > > > > > >From: Sasha Pachev <[EMAIL PROTECTED]> > > > >To: shaun thornburgh <[EMAIL PROTECTED]> > > > >CC: mysql@lists.mysql.com > > > >Subject: Re: Help with a join query please! > > > >Date: Tue, 21 Dec 2004 14:57:43 -0700 > > > > > > > >shaun thornburgh wrote: > > > >>Hi, > > > >> > > > >>I have (among others) three tables in my database that i am struggling > > > > > >>with a join query on. The database holds dates for Bookings. If Users > >are > > > >>Allocated to a particular Project they can be booked. However if a > >user is > > > >>booked but then unallocated I want to be able to display all peolple > > > >>allocated to that project plus the person originally booked. Here are > >my > > > >>efforts so far: > > > >> > > > >>SELECT U.User_ID, U.User_Firstname, U.User_Lastname > > > >>FROM Allocations A, Users U > > > >>LEFT JOIN Bookings B > > > >>ON B.User_ID = U.User_ID > > > >>AND B.Booking_ID = '4512' > > > >>WHERE U.User_ID = A.User_ID > > > >>AND A.Project_ID = '11' > > > >>ORDER BY User_Firstname; > > > > > > > >Shaun: > > > > > > > >If I understand the problem right, it sounds like you are missing AND > > > >B.Bookings_ID is NULL in the where clause. > > > > > > > > > > > > > > > >-- > > > >Sasha Pachev > > > >Create online surveys at http://www.surveyz.com/ > > > > > > > >-- > > > >MySQL General Mailing List > > > >For list archives: http://lists.mysql.com/mysql > > > >To unsubscribe: > > > >http://lists.mysql.com/[EMAIL PROTECTED] > > > > > > > > > > > > > > > > -- > > > MySQL General Mailing List > > > For list archives: http://lists.mysql.com/mysql > > > To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED] > > > > >
Re: Help with a join query please!
Hi Shawn, Thanks for your reply, but your query seems to return everyone allocated to that project plus everyone who has ever been booked on that project. What I need is everyone who is currently allocated to that project and the staff member who has been booked for the booking in question whether they have been allocated to the project or not. In reply to your quesion bookings don't relate to allocations directly. A booking relates to a project by Project_ID and to a user by User_ID. Allocations contains both of these fields to determine who is 'Allocated' to a particular project. Thanks again for your help! From: [EMAIL PROTECTED] To: "shaun thornburgh" <[EMAIL PROTECTED]> CC: mysql@lists.mysql.com,[EMAIL PROTECTED] Subject: Re: Help with a join query please! Date: Thu, 23 Dec 2004 10:14:24 -0500 So -- what's the field that relates a booking to an allocation? Do they share a project_ID or what? If they do, you might try this: SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname FROM Users U LEFT JOIN Allocations A on A.User_ID = U.User_ID LEFT JOIN Bookings B ON B.User_ID = U.User_ID WHERE A.Project_ID = '11' OR B.Project_ID = '11' ORDER BY User_Firstname; A pet peeve of mine is when people 'quote' NUMBERS. According to the extremely well written manual, you only need to quote STRING values and DATETIME values. Unless the columns Project_ID and Booking_ID are some form of STRING column (CHAR, VARCHAR, TEXT, etc.) you don't need to quote their values in queries. It forces the query engine to perform an unnecessary internal type conversion. Here is what I think your query should look like: SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname FROM Users U LEFT JOIN Allocations A on A.User_ID = U.User_ID LEFT JOIN Bookings B ON B.User_ID = U.User_ID WHERE A.Project_ID = 11 OR B.Project_ID = 11 ORDER BY User_Firstname; I used SELECT DISTINCT so that in the event that someone was both BOOKED and ALLOCATED to the same project, you only got them listed once. Shawn Green Database Administrator Unimin Corporation - Spruce Pine "shaun thornburgh" <[EMAIL PROTECTED]> wrote on 12/23/2004 08:37:37 AM: > Hi, > > Thanks for your reply but that produces exactly the same result... > > Any ideas? > > >From: Sasha Pachev <[EMAIL PROTECTED]> > >To: shaun thornburgh <[EMAIL PROTECTED]> > >CC: mysql@lists.mysql.com > >Subject: Re: Help with a join query please! > >Date: Tue, 21 Dec 2004 14:57:43 -0700 > > > >shaun thornburgh wrote: > >>Hi, > >> > >>I have (among others) three tables in my database that i am struggling > >>with a join query on. The database holds dates for Bookings. If Users are > >>Allocated to a particular Project they can be booked. However if a user is > >>booked but then unallocated I want to be able to display all peolple > >>allocated to that project plus the person originally booked. Here are my > >>efforts so far: > >> > >>SELECT U.User_ID, U.User_Firstname, U.User_Lastname > >>FROM Allocations A, Users U > >>LEFT JOIN Bookings B > >>ON B.User_ID = U.User_ID > >>AND B.Booking_ID = '4512' > >>WHERE U.User_ID = A.User_ID > >>AND A.Project_ID = '11' > >>ORDER BY User_Firstname; > > > >Shaun: > > > >If I understand the problem right, it sounds like you are missing AND > >B.Bookings_ID is NULL in the where clause. > > > > > > > >-- > >Sasha Pachev > >Create online surveys at http://www.surveyz.com/ > > > >-- > >MySQL General Mailing List > >For list archives: http://lists.mysql.com/mysql > >To unsubscribe: > >http://lists.mysql.com/[EMAIL PROTECTED] > > > > > > -- > MySQL General Mailing List > For list archives: http://lists.mysql.com/mysql > To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED] > -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Help with a join query please!
So -- what's the field that relates a booking to an allocation? Do they share a project_ID or what? If they do, you might try this: SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname FROM Users U LEFT JOIN Allocations A on A.User_ID = U.User_ID LEFT JOIN Bookings B ON B.User_ID = U.User_ID WHERE A.Project_ID = '11' OR B.Project_ID = '11' ORDER BY User_Firstname; A pet peeve of mine is when people 'quote' NUMBERS. According to the extremely well written manual, you only need to quote STRING values and DATETIME values. Unless the columns Project_ID and Booking_ID are some form of STRING column (CHAR, VARCHAR, TEXT, etc.) you don't need to quote their values in queries. It forces the query engine to perform an unnecessary internal type conversion. Here is what I think your query should look like: SELECT DISTINCT U.User_ID, U.User_Firstname, U.User_Lastname FROM Users U LEFT JOIN Allocations A on A.User_ID = U.User_ID LEFT JOIN Bookings B ON B.User_ID = U.User_ID WHERE A.Project_ID = 11 OR B.Project_ID = 11 ORDER BY User_Firstname; I used SELECT DISTINCT so that in the event that someone was both BOOKED and ALLOCATED to the same project, you only got them listed once. Shawn Green Database Administrator Unimin Corporation - Spruce Pine "shaun thornburgh" <[EMAIL PROTECTED]> wrote on 12/23/2004 08:37:37 AM: > Hi, > > Thanks for your reply but that produces exactly the same result... > > Any ideas? > > >From: Sasha Pachev <[EMAIL PROTECTED]> > >To: shaun thornburgh <[EMAIL PROTECTED]> > >CC: mysql@lists.mysql.com > >Subject: Re: Help with a join query please! > >Date: Tue, 21 Dec 2004 14:57:43 -0700 > > > >shaun thornburgh wrote: > >>Hi, > >> > >>I have (among others) three tables in my database that i am struggling > >>with a join query on. The database holds dates for Bookings. If Users are > >>Allocated to a particular Project they can be booked. However if a user is > >>booked but then unallocated I want to be able to display all peolple > >>allocated to that project plus the person originally booked. Here are my > >>efforts so far: > >> > >>SELECT U.User_ID, U.User_Firstname, U.User_Lastname > >>FROM Allocations A, Users U > >>LEFT JOIN Bookings B > >>ON B.User_ID = U.User_ID > >>AND B.Booking_ID = '4512' > >>WHERE U.User_ID = A.User_ID > >>AND A.Project_ID = '11' > >>ORDER BY User_Firstname; > > > >Shaun: > > > >If I understand the problem right, it sounds like you are missing AND > >B.Bookings_ID is NULL in the where clause. > > > > > > > >-- > >Sasha Pachev > >Create online surveys at http://www.surveyz.com/ > > > >-- > >MySQL General Mailing List > >For list archives: http://lists.mysql.com/mysql > >To unsubscribe: > >http://lists.mysql.com/[EMAIL PROTECTED] > > > > > > -- > MySQL General Mailing List > For list archives: http://lists.mysql.com/mysql > To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED] >
Re: Help with a join query please!
Hi, Thanks for your reply but that produces exactly the same result... Any ideas? From: Sasha Pachev <[EMAIL PROTECTED]> To: shaun thornburgh <[EMAIL PROTECTED]> CC: mysql@lists.mysql.com Subject: Re: Help with a join query please! Date: Tue, 21 Dec 2004 14:57:43 -0700 shaun thornburgh wrote: Hi, I have (among others) three tables in my database that i am struggling with a join query on. The database holds dates for Bookings. If Users are Allocated to a particular Project they can be booked. However if a user is booked but then unallocated I want to be able to display all peolple allocated to that project plus the person originally booked. Here are my efforts so far: SELECT U.User_ID, U.User_Firstname, U.User_Lastname FROM Allocations A, Users U LEFT JOIN Bookings B ON B.User_ID = U.User_ID AND B.Booking_ID = '4512' WHERE U.User_ID = A.User_ID AND A.Project_ID = '11' ORDER BY User_Firstname; Shaun: If I understand the problem right, it sounds like you are missing AND B.Bookings_ID is NULL in the where clause. -- Sasha Pachev Create online surveys at http://www.surveyz.com/ -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED] -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Help with a join query please!
shaun thornburgh wrote: Hi, I have (among others) three tables in my database that i am struggling with a join query on. The database holds dates for Bookings. If Users are Allocated to a particular Project they can be booked. However if a user is booked but then unallocated I want to be able to display all peolple allocated to that project plus the person originally booked. Here are my efforts so far: SELECT U.User_ID, U.User_Firstname, U.User_Lastname FROM Allocations A, Users U LEFT JOIN Bookings B ON B.User_ID = U.User_ID AND B.Booking_ID = '4512' WHERE U.User_ID = A.User_ID AND A.Project_ID = '11' ORDER BY User_Firstname; Shaun: If I understand the problem right, it sounds like you are missing AND B.Bookings_ID is NULL in the where clause. -- Sasha Pachev Create online surveys at http://www.surveyz.com/ -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Help with a join query please!
Hi, I have (among others) three tables in my database that i am struggling with a join query on. The database holds dates for Bookings. If Users are Allocated to a particular Project they can be booked. However if a user is booked but then unallocated I want to be able to display all peolple allocated to that project plus the person originally booked. Here are my efforts so far: SELECT U.User_ID, U.User_Firstname, U.User_Lastname FROM Allocations A, Users U LEFT JOIN Bookings B ON B.User_ID = U.User_ID AND B.Booking_ID = '4512' WHERE U.User_ID = A.User_ID AND A.Project_ID = '11' ORDER BY User_Firstname; I am using version 3.23.54. The query works but doesnt return the required values. The query returns all of the users allocated to the project, not the user who is currently booked but not allocated to the project. Here are the table definitions: mysql> DESCRIBE Allocations; ++-+--+-+-+---+ | Field | Type| Null | Key | Default | Extra | ++-+--+-+-+---+ | Project_ID | int(11) | | PRI | 0 | | | User_ID| int(11) | | PRI | 0 | | ++-+--+-+-+---+ 2 rows in set (0.00 sec) mysql> DESCRIBE Bookings; +-+-+--+-+-++ | Field | Type| Null | Key | Default | Extra | +-+-+--+-+-++ | Booking_ID | int(11) | | PRI | NULL| auto_increment | | Booking_Type| varchar(15) | | | Unavailability | | | User_ID | int(11) | | | 0 | | | Project_ID | int(11) | YES | | NULL| | | Rep_ID | int(11) | YES | | NULL| | | PCT_ID | int(11) | YES | | NULL| | | Practice_ID | int(11) | YES | | NULL| | | Booking_Creator_ID | int(11) | YES | | NULL| | | Booking_Creation_Date | datetime| YES | | NULL| | | Booking_Start_Date | datetime| | | -00-00 00:00:00 | | | Booking_End_Date| datetime| | | -00-00 00:00:00 | | | Booking_Completion_Date | date| YES | | NULL| | | Booking_Mileage | int(5) | YES | | NULL| | | Booking_Status | varchar(15) | | | Other | | | Unavailability_ID | int(2) | YES | | NULL| | | Work_Type_ID| int(2) | YES | | NULL| | | Additional_Notes| text| YES | | NULL| | | Form_Recieved | char(3) | | | | | | Section_C | char(3) | | | | | | Date_Difference | varchar(20) | | | n/a | | +-+-+--+-+-++ 20 rows in set (0.00 sec) mysql> DESCRIBE Projects; +--+--+--+-+-++ | Field| Type | Null | Key | Default | Extra | +--+--+--+-+-++ | Project_ID | int(11) | | PRI | NULL| auto_increment | | Project_Name | varchar(100) | | | || | Client_ID| int(11) | | | 0 || | Rep_Viewable | char(3) | | | Yes || | User_ID | int(11) | YES | | NULL|| +--+--+--+-+-++ 5 rows in set (0.00 sec) mysql> DESCRIBE Users; +--+--+--+-+-++ | Field| Type | Null | Key | Default | Extra | +--+--+--+-+-++ | User_ID | int(11) | | PRI | NULL| auto_increment | | Client_ID| int(3) | YES | | NULL| | | User_Username| varchar(40) | | | | | | User_Firstname | varchar(50) | YES | | NULL| | | User_Lastname| varchar(50) | YES | | NULL| | | User_Password| varchar(20) | YES
Re: Help with a join query please
From: "Rhino" <[EMAIL PROTECTED]> To: "shaun thornburgh" <[EMAIL PROTECTED]>,<[EMAIL PROTECTED]> Subject: Re: Help with a join query please Date: Thu, 16 Dec 2004 14:59:48 -0500 - Original Message - From: "shaun thornburgh" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, December 16, 2004 2:02 PM Subject: Help with a join query please > Hi, > > I have (among others) three tables in my database that i am struggling with > a join query on. The database holds dates for Bookings. If Users are > Allocated to a particular Project they can be booked. However if a user is > booked but then unallocated I want to be able to display all peolple > allocated to that project plus the person originally booked. Here are my > efforts so far: > > SELECT U.User_ID, U.User_Firstname, U.User_Lastname > FROM Allocations A, Users U > LEFT JOIN Bookings B > ON B.User_ID = U.User_ID > AND B.Booking_ID = '4512' > WHERE U.User_ID = A.User_ID > AND A.Project_ID = '11' > ORDER BY User_Firstname; > > I hope this makes sense! > We could do a lot more for you if you mentioned a few things: a) which version of MySQL you are using b) whether the query works or not the way it stands c) if it does work, what is wrong with the answer you are getting d) if it doesn't work, what error message you are getting And if you REALLY want to help us help you, you could provide definitions of the tables and a few rows of typical data from each table to help us visualize the problem better. Rhino Sorry guys! a) I am using version 3.23.54 b) The query works but doesnt return the required values c) The query returns all of the users allocated to the project, not the user who is currently booked but not allocated to the project d) No error message! Here are the table definitions: mysql> DESCRIBE Allocations; ++-+--+-+-+---+ | Field | Type| Null | Key | Default | Extra | ++-+--+-+-+---+ | Project_ID | int(11) | | PRI | 0 | | | User_ID| int(11) | | PRI | 0 | | ++-+--+-+-+---+ 2 rows in set (0.00 sec) mysql> DESCRIBE Bookings; +-+-+--+-+-++ | Field | Type| Null | Key | Default | Extra | +-+-+--+-+-++ | Booking_ID | int(11) | | PRI | NULL| auto_increment | | Booking_Type| varchar(15) | | | Unavailability | | | User_ID | int(11) | | | 0 | | | Project_ID | int(11) | YES | | NULL| | | Rep_ID | int(11) | YES | | NULL| | | PCT_ID | int(11) | YES | | NULL| | | Practice_ID | int(11) | YES | | NULL| | | Booking_Creator_ID | int(11) | YES | | NULL| | | Booking_Creation_Date | datetime| YES | | NULL| | | Booking_Start_Date | datetime| | | -00-00 00:00:00 | | | Booking_End_Date| datetime| | | -00-00 00:00:00 | | | Booking_Completion_Date | date| YES | | NULL| | | Booking_Mileage | int(5) | YES | | NULL| | | Booking_Status | varchar(15) | | | Other | | | Unavailability_ID | int(2) | YES | | NULL| | | Work_Type_ID| int(2) | YES | | NULL| | | Additional_Notes| text| YES | | NULL| | | Form_Recieved | char(3) | | | | | | Section_C | char(3) | | | | | | Date_Difference | varchar(20) | | | n/a | | +-+-+--+-+-++ 20 rows in set (0.00 sec) mysql> DESCRIBE Projects; +--+--+--+-+-++ | Field| Type | Null | Key | Default | Extra | +--+--+--+-+-++ | Project_ID | int(11) | | PRI | NULL| auto_increment | | Project_Nam
Re: Help with a join query please
- Original Message - From: "shaun thornburgh" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, December 16, 2004 2:02 PM Subject: Help with a join query please > Hi, > > I have (among others) three tables in my database that i am struggling with > a join query on. The database holds dates for Bookings. If Users are > Allocated to a particular Project they can be booked. However if a user is > booked but then unallocated I want to be able to display all peolple > allocated to that project plus the person originally booked. Here are my > efforts so far: > > SELECT U.User_ID, U.User_Firstname, U.User_Lastname > FROM Allocations A, Users U > LEFT JOIN Bookings B > ON B.User_ID = U.User_ID > AND B.Booking_ID = '4512' > WHERE U.User_ID = A.User_ID > AND A.Project_ID = '11' > ORDER BY User_Firstname; > > I hope this makes sense! > We could do a lot more for you if you mentioned a few things: a) which version of MySQL you are using b) whether the query works or not the way it stands c) if it does work, what is wrong with the answer you are getting d) if it doesn't work, what error message you are getting And if you REALLY want to help us help you, you could provide definitions of the tables and a few rows of typical data from each table to help us visualize the problem better. Rhino -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Help with a join query please
Hi, I have (among others) three tables in my database that i am struggling with a join query on. The database holds dates for Bookings. If Users are Allocated to a particular Project they can be booked. However if a user is booked but then unallocated I want to be able to display all peolple allocated to that project plus the person originally booked. Here are my efforts so far: SELECT U.User_ID, U.User_Firstname, U.User_Lastname FROM Allocations A, Users U LEFT JOIN Bookings B ON B.User_ID = U.User_ID AND B.Booking_ID = '4512' WHERE U.User_ID = A.User_ID AND A.Project_ID = '11' ORDER BY User_Firstname; I hope this makes sense! Shaun -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]