Re: [PHP-DB] Left Join
Maybe it's just me, but I can't see anything that would work as foreign key for you to join on - neither table seems to have a foreign key On 7 August 2011 21:53, Chris Stinemetz chrisstinem...@gmail.com wrote: I am tyring to build a query that will take the most recent stores.store_date column then join it with store_list.store_name where store_list.store_name and stores.store_subject match then return the most recent stores.store_date. I am guessing this will require some sort of join. I have been tyring to figure it out but have had no luck. Any help is greatly apprciated. Thank you. mysql select * from stores; +--+-++-+---+--+-+-- ---+ | store_id | store_subject | store_type | store_date | store_mar | store_by | store_tptest | store_comment s | +--+-++-+---+--+-+-- ---+ | 78 | Bella Roe | corporate | 2011-06-28 15:01:02 | 0 | 1 | | | | 79 | Bella | corporate | 2011-08-06 08:48:26 | 0 | 1 | 600kbps-3.8mbps | test | | 80 | Brittney~2120 | corporate | 2011-08-06 08:48:50 | 1 | 1 | 600kbps-3.8mbps | should post t o Wichita! | | 81 | Motor | premier | 2011-08-06 14:52:55 | 1 | 1 | 600kbps-3.8mbps | test | | 82 | Liberty | corporate | 2011-08-06 14:54:41 | 0 | 1 | 0-250kbps | test | | 83 | Seneca~3165 | corporate | 2011-08-06 14:58:47 | 0 | 1 | 600kbps-3.8mbps | test | | 84 | Liberty | corporate | 2011-08-06 14:59:32 | 0 | 1 | 400-600kbps | test | | 85 | Brittney~2120 | corporate | 2011-08-06 15:00:43 | 0 | 1 | 600kbps-3.8mbps | test | | 86 | Liberty | corporate | 2011-08-06 15:01:11 | 0 | 1 | 300-400kbps | test | | 87 | Brittney~2120 | corporate | 2011-08-06 15:11:06 | 0 | 1 | 250-300kbps | test | | 88 | Brittney~2120 | corporate | 2011-08-06 15:23:53 | 1 | 1 | 600kbps-3.8mbps | test | | 89 | Bella | corporate | 2011-08-06 15:31:49 | 0 | 1 | 600kbps-3.8mbps | test | | 90 | Cleartalk~10221 | premier | 2011-08-06 15:32:54 | 0 | 1 | 250-300kbps | test | | 91 | Chit | premier | 2011-08-06 15:33:50 | 1 | 1 | 250-300kbps | test | | 92 | | | 2011-08-06 15:34:21 | 2 | 1 | 300-400kbps | test | | 93 | Bella | corporate | 2011-08-07 11:26:13 | 0 | 1 | 600kbps-3.8mbps | test | +--+-++-+---+--+-+-- ---+ 16 rows in set (0.00 sec) mysql mysql select * from store_list LIMIT 15;' ++---+++-+---+ | id | market_prefix | store_name | store_type | market_name | id_market | ++---+++-+---+ | 1 | MCI | Bella Roe~4980 Roe Blvd | Corporate | Kansas City | 0 | | 2 | MCI | Cleartalk~4635 Shawnee Dr | Premier | Kansas City | 0 | | 3 | MCI | Cleartalk~3612 State Avenue | Premier | Kansas City | 0 | | 4 | MCI | Endless Wireless~1620 Central Avenue | Premier | Kansas City | 0 | | 5 | MCI | Get Wireless~840 Minnesota | Premier | Kansas City | 0 | | 6 | MCI | Ring Ring Wireless~7559 State Avenue | Premier | Kansas City | 0 | | 7 | MCI | Cleartalk~1212 Sante Fe | Premier | Kansas City | 0 | | 8 | MCI | Cleartalk~10221 W 75th Street | Premier | Kansas City | 0 | | 9 | MCI | Free Talk~10830 Shawnee Mission Pkwy | Premier | Kansas City | 0 | | 10 | MCI | Loma Vista~8712 Blue Ridge Blvd | Corporate | Kansas City | 0 | | 11 | MCI | Ring Ring Wireless~25 W. 39th Street | Premier | Kansas City | 0 | | 12 | MCI | Ring Ring Wireless~3039 Prospect Ave | Premier | Kansas City | 0 | | 13 | MCI | Ring Ring Wireless~1201 E Linwood Blvd | Premier | Kansas City | 0 | | 14 | MCI | Ring Ring
Re: [PHP-DB] Left Join
On Sun, Aug 7, 2011 at 3:00 PM, Peter Lind peter.e.l...@gmail.com wrote: Maybe it's just me, but I can't see anything that would work as foreign key for you to join on - neither table seems to have a foreign key Sorry for my ignorance. How do I create the foreign key? The two columns from each table that have a similar relationship are stores.store_mar and store_list.id_market. Once the foreign key is build what would be the correct syntax to achieve my query? Thank you very much, Chris
RE: [PHP-DB] Left Join
Is the design under your control? If so, you need to change it. Your store_list.store_name field breaks the first rule of relational databases - it isn't atomic. That means that you have two pieces of information there - the store name and store address. You even have a '~' there to separate them (nice try, but no). It's not clear what the function of the 'stores' table is, but there's a nasty smell coming from there as well (and a field with tilde-separated data too!) If this *is* your design, then you might want to show what the source data looks like. If it's not your design, you have my permission to tell the owner off ;-) Not that this really has anything to do with PHP, but I won't say anything if you won't. Toby -Original Message- From: Chris Stinemetz [mailto:chrisstinem...@gmail.com] Sent: Sunday, August 07, 2011 9:33 PM To: Peter Lind Cc: php-db@lists.php.net Subject: Re: [PHP-DB] Left Join On Sun, Aug 7, 2011 at 3:00 PM, Peter Lind peter.e.l...@gmail.com wrote: Maybe it's just me, but I can't see anything that would work as foreign key for you to join on - neither table seems to have a foreign key Sorry for my ignorance. How do I create the foreign key? The two columns from each table that have a similar relationship are stores.store_mar and store_list.id_market. Once the foreign key is build what would be the correct syntax to achieve my query? Thank you very much, Chris -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Left Join with more than two tables......
The left join works the same for two or more tables. select * from table1 left join table2 on table1.id=table2.id left join table3 on table1.id=table3.id ... Just specify the table you want to join and what to use to join them. On Tuesday, August 12, 2003, at 03:16 PM, Michael Cortes wrote: I know that we can .. select last_name,first_name, table2.school from table1 left join table2 on school_id=bldg_id Listing all students and giving the school name for all students, if they have one. If not, it lists the kid anyway and leaves that field blank. Anyhow, how about multiple tables? I can't find it with google. I have : table1 with the students table2 with library PINs and an autoincrement id table3 with a lunch status and an autoincrement id I want to list all students (I will select just certain fields) but if they have it, I want to list the PIN from table2 and the lunch status from table3. I can do it as a join but then anystudent who doesn't cross-reference gets left out. I need to list all students. -- Brent Baisley Systems Architect Landover Associates, Inc. Search Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Left Join with more than two tables......
Thanks. I couldn't find the correct syntax in my resources. I tried at one point to use commas like: select * from table1 left join table2,table3 on table1.id=table2.id,table1.id=table3.id Thanks again for the correct syntax. On Tuesday 12 August 2003 04:00 pm, Brent Baisley wrote: The left join works the same for two or more tables. select * from table1 left join table2 on table1.id=table2.id left join table3 on table1.id=table3.id ... Just specify the table you want to join and what to use to join them. On Tuesday, August 12, 2003, at 03:16 PM, Michael Cortes wrote: I know that we can .. select last_name,first_name, table2.school from table1 left join table2 on school_id=bldg_id Listing all students and giving the school name for all students, if they have one. If not, it lists the kid anyway and leaves that field blank. Anyhow, how about multiple tables? I can't find it with google. I have : table1 with the students table2 with library PINs and an autoincrement id table3 with a lunch status and an autoincrement id I want to list all students (I will select just certain fields) but if they have it, I want to list the PIN from table2 and the lunch status from table3. I can do it as a join but then anystudent who doesn't cross-reference gets left out. I need to list all students. -- Michael Cortes Fort LeBoeuf School District 34 East Ninth Street PO Box 810 Waterford PA 16441-0810 814.796.4795 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] LEFT JOIN not working
Should work (provided that the HAVING clauses be written after the GROUP BY) But less efficient. The point of using the WHERE clauses is to restrict during the JOIN. What you propose amounts to joining the full left table, which may be very expensive, especially in the case of web log data. Ignatius - Original Message - From: Henrik Hornemann [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED] Cc: PHP-DB [EMAIL PROTECTED] Sent: Wednesday, January 15, 2003 10:56 AM Subject: SV: [PHP-DB] LEFT JOIN not working Hi, You might want to try something like: SELECT ads_displayrate.name, SUM(ads_displayrate.count) as display, SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name HAVING YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' AND DAYOFMONTH(ads_displayrate.date) = '05' GROUP BY ads_displayrate.name ORDER BY ads_displayrate.name regards Henrik Hornemann -Oprindelig meddelelse- Fra: Lisi [mailto:[EMAIL PROTECTED]] Sendt: 14. januar 2003 18:47 Til: Ignatius Reilly Cc: PHP-DB Emne: Re: [PHP-DB] LEFT JOIN not working Still not working. I made the change, and I'm still getting all results. I even tried it without the leading '0' in front of the 1, no good. Here's my current query, with suggested changes: SELECT ads_displayrate.name, SUM(ads_displayrate.count) as display, SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name AND YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' AND DAYOFMONTH(ads_displayrate.date) = '05' GROUP BY ads_displayrate.name ORDER BY ads_displayrate.name ads_displayrate.date is a column of date type, so as far as I understand this should work. Is there some typo I'm missing? At 07:15 PM 1/9/03 +0100, you wrote: Oops! I missed again. If you specify conditions pertaining to the right-hand table, such as: ads_clickrate.date = '2001', Then you will lose all result rows for which the right-hand data is NULL. Not the expected result. So your restricting WHERE clauses must apply to the left-hand table only. Therefore: WHERE YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' (if your table ads_displayrate has such date fields). HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 6:54 PM Subject: Re: [PHP-DB] LEFT JOIN not working Cool! It's mostly working now, the only problem is it's ignoring the other clauses in the ON clause that select the desired date. Perhaps it's not supposed to be connected this way? How would I select specific dates? Thanks again, -Lisi At 01:20 PM 1/9/03 +0100, you wrote: Oops! Sorry, I missed it the first time. Your query should start as: SELECT ads_displayrate.name instead of SELECT ads_clickrate.name then you will always have a non-NULL name (coming from the table on the left of the LEFT JOIN). HTH Ignatius, from Brussels Where the fuck is Belgium? D. Ivester, CEO, Coca Cola - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 1:11 PM Subject: Re: [PHP-DB] LEFT JOIN not working Exactly my question - why does it not have a name? How would I modify my query to get a row with a name but null value for date? I thought the join would take care of this, but I'm obviously not doing it right. You mention a unique identifier, there is a separate table with a row for each ad, containing name, URL, and a unique ID number (autoincrement). Should this table be included somehow in the query? How would this help? Thanks, -Lisi At 12:45 PM 1/9/03 +0100, Ignatius Reilly wrote: Your 4th row ought to have an identifier of some sort. From your SELECT statement, this seems to be the name. Why does it not have a name? Probably what you want is a row with a name but a NULL value for ads_clickrate.date. (by the way it is EXTREMELY advisable to use an abstract identifier, such as an id, unique and required, instead of name) Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 12:18 PM Subject: Re: [PHP-DB] LEFT JOIN not working OK, this helped a bit. Now I have, in addition
Re: [PHP-DB] LEFT JOIN not working
Still not working. I made the change, and I'm still getting all results. I even tried it without the leading '0' in front of the 1, no good. Here's my current query, with suggested changes: SELECT ads_displayrate.name, SUM(ads_displayrate.count) as display, SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name AND YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' AND DAYOFMONTH(ads_displayrate.date) = '05' GROUP BY ads_displayrate.name ORDER BY ads_displayrate.name ads_displayrate.date is a column of date type, so as far as I understand this should work. Is there some typo I'm missing? At 07:15 PM 1/9/03 +0100, you wrote: Oops! I missed again. If you specify conditions pertaining to the right-hand table, such as: ads_clickrate.date = '2001', Then you will lose all result rows for which the right-hand data is NULL. Not the expected result. So your restricting WHERE clauses must apply to the left-hand table only. Therefore: WHERE YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' (if your table ads_displayrate has such date fields). HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 6:54 PM Subject: Re: [PHP-DB] LEFT JOIN not working Cool! It's mostly working now, the only problem is it's ignoring the other clauses in the ON clause that select the desired date. Perhaps it's not supposed to be connected this way? How would I select specific dates? Thanks again, -Lisi At 01:20 PM 1/9/03 +0100, you wrote: Oops! Sorry, I missed it the first time. Your query should start as: SELECT ads_displayrate.name instead of SELECT ads_clickrate.name then you will always have a non-NULL name (coming from the table on the left of the LEFT JOIN). HTH Ignatius, from Brussels Where the fuck is Belgium? D. Ivester, CEO, Coca Cola - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 1:11 PM Subject: Re: [PHP-DB] LEFT JOIN not working Exactly my question - why does it not have a name? How would I modify my query to get a row with a name but null value for date? I thought the join would take care of this, but I'm obviously not doing it right. You mention a unique identifier, there is a separate table with a row for each ad, containing name, URL, and a unique ID number (autoincrement). Should this table be included somehow in the query? How would this help? Thanks, -Lisi At 12:45 PM 1/9/03 +0100, Ignatius Reilly wrote: Your 4th row ought to have an identifier of some sort. From your SELECT statement, this seems to be the name. Why does it not have a name? Probably what you want is a row with a name but a NULL value for ads_clickrate.date. (by the way it is EXTREMELY advisable to use an abstract identifier, such as an id, unique and required, instead of name) Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 12:18 PM Subject: Re: [PHP-DB] LEFT JOIN not working OK, this helped a bit. Now I have, in addition to the three rows of ads that have ben clicked on, a fourth row with no ad name, 0 clickthroughs, and 24 displays. That plus the other three account for all the displayed ads. However, since it is returning a null value for any ad name that has not been clicked on, and then it's grouped by ad name, it lumps all non-clicked ads into one row. What I need is to see each ad on a separate row, which is what I thought a LEFT JOIN was supposed to do. Any suggestions? Thanks, -Lisi At 11:07 AM 1/9/03 +0100, Ignatius Reilly wrote: problem 1: move the WHERE clauses to the ON clauses problem 2: Obviously your intent with COUNT(ads_clickrate.date) is to count the number of non-null occurences of click. But COUNT() is not the appropriate function to do this (it will simply give you the number of rows inside a group). Try replacing COUNT(ads_clickrate.date) by SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 10:44 AM Subject: [PHP-DB] LEFT JOIN not working I have a page with many ads that stores both the number of times
Re: [PHP-DB] LEFT JOIN not working
For a start simplify the query, so that you are returning only one field until you get it right, say ads_displayrate.name. Second, echo the SQL statement so you can see what has been generated. Third, check the datatypes returned for YEAR(), MOTN() and DAYOFMONTH() functions in MySQL. As part of your diagnosis you may want to include the generated output from these functions so you see what they are returning. Fourth, if you have access to the MySQL console, try this interactively. HTH - Miles Thompson At 07:46 PM 1/14/2003 +0200, Lisi wrote: Still not working. I made the change, and I'm still getting all results. I even tried it without the leading '0' in front of the 1, no good. Here's my current query, with suggested changes: SELECT ads_displayrate.name, SUM(ads_displayrate.count) as display, SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name AND YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' AND DAYOFMONTH(ads_displayrate.date) = '05' GROUP BY ads_displayrate.name ORDER BY ads_displayrate.name ads_displayrate.date is a column of date type, so as far as I understand this should work. Is there some typo I'm missing? At 07:15 PM 1/9/03 +0100, you wrote: Oops! I missed again. If you specify conditions pertaining to the right-hand table, such as: ads_clickrate.date = '2001', Then you will lose all result rows for which the right-hand data is NULL. Not the expected result. So your restricting WHERE clauses must apply to the left-hand table only. Therefore: WHERE YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' (if your table ads_displayrate has such date fields). HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 6:54 PM Subject: Re: [PHP-DB] LEFT JOIN not working Cool! It's mostly working now, the only problem is it's ignoring the other clauses in the ON clause that select the desired date. Perhaps it's not supposed to be connected this way? How would I select specific dates? Thanks again, -Lisi At 01:20 PM 1/9/03 +0100, you wrote: Oops! Sorry, I missed it the first time. Your query should start as: SELECT ads_displayrate.name instead of SELECT ads_clickrate.name then you will always have a non-NULL name (coming from the table on the left of the LEFT JOIN). HTH Ignatius, from Brussels Where the fuck is Belgium? D. Ivester, CEO, Coca Cola - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 1:11 PM Subject: Re: [PHP-DB] LEFT JOIN not working Exactly my question - why does it not have a name? How would I modify my query to get a row with a name but null value for date? I thought the join would take care of this, but I'm obviously not doing it right. You mention a unique identifier, there is a separate table with a row for each ad, containing name, URL, and a unique ID number (autoincrement). Should this table be included somehow in the query? How would this help? Thanks, -Lisi At 12:45 PM 1/9/03 +0100, Ignatius Reilly wrote: Your 4th row ought to have an identifier of some sort. From your SELECT statement, this seems to be the name. Why does it not have a name? Probably what you want is a row with a name but a NULL value for ads_clickrate.date. (by the way it is EXTREMELY advisable to use an abstract identifier, such as an id, unique and required, instead of name) Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 12:18 PM Subject: Re: [PHP-DB] LEFT JOIN not working OK, this helped a bit. Now I have, in addition to the three rows of ads that have ben clicked on, a fourth row with no ad name, 0 clickthroughs, and 24 displays. That plus the other three account for all the displayed ads. However, since it is returning a null value for any ad name that has not been clicked on, and then it's grouped by ad name, it lumps all non-clicked ads into one row. What I need is to see each ad on a separate row, which is what I thought a LEFT JOIN was supposed to do. Any suggestions? Thanks, -Lisi At 11:07 AM 1/9/03 +0100, Ignatius Reilly wrote: problem 1: move the WHERE clauses to the ON clauses problem 2: Obviously your intent with COUNT(ads_clickrate.date) is to count the number of non-null occurences of click. But COUNT() is not the appropriate
Re: [PHP-DB] LEFT JOIN not working
problem 1: move the WHERE clauses to the ON clauses problem 2: Obviously your intent with COUNT(ads_clickrate.date) is to count the number of non-null occurences of click. But COUNT() is not the appropriate function to do this (it will simply give you the number of rows inside a group). Try replacing COUNT(ads_clickrate.date) by SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 10:44 AM Subject: [PHP-DB] LEFT JOIN not working I have a page with many ads that stores both the number of times an ad is displayed and how many times it gets clicked. These are stored in two different tables (since different information is stored for each) but both have identical name columns. I am trying to display both # times displayed and # times clicked in the same table in an admin page. Here is my query: SELECT ads_clickrate.name, ads_clickrate.link, SUM(ads_displayrate.count) as display, COUNT(ads_clickrate.date) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name WHERE YEAR(ads_clickrate.date) = '2003' AND MONTH(ads_clickrate.date) = '01' GROUP BY ads_clickrate.name ORDER BY ads_clickrate.name I want to display for each ad the number of times displayed, and then number of times clicked if applicable, if not 0. The query is only returning rows for ads that have been clicked on. Is the problem because I have a COUNT column for one table, with a group by? Is this causing the display to be grouped also? If you need more information how the table is set up let me know, I'm really stumped here. Thanks, -Lisi -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] LEFT JOIN not working
Your 4th row ought to have an identifier of some sort. From your SELECT statement, this seems to be the name. Why does it not have a name? Probably what you want is a row with a name but a NULL value for ads_clickrate.date. (by the way it is EXTREMELY advisable to use an abstract identifier, such as an id, unique and required, instead of name) Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 12:18 PM Subject: Re: [PHP-DB] LEFT JOIN not working OK, this helped a bit. Now I have, in addition to the three rows of ads that have ben clicked on, a fourth row with no ad name, 0 clickthroughs, and 24 displays. That plus the other three account for all the displayed ads. However, since it is returning a null value for any ad name that has not been clicked on, and then it's grouped by ad name, it lumps all non-clicked ads into one row. What I need is to see each ad on a separate row, which is what I thought a LEFT JOIN was supposed to do. Any suggestions? Thanks, -Lisi At 11:07 AM 1/9/03 +0100, Ignatius Reilly wrote: problem 1: move the WHERE clauses to the ON clauses problem 2: Obviously your intent with COUNT(ads_clickrate.date) is to count the number of non-null occurences of click. But COUNT() is not the appropriate function to do this (it will simply give you the number of rows inside a group). Try replacing COUNT(ads_clickrate.date) by SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 10:44 AM Subject: [PHP-DB] LEFT JOIN not working I have a page with many ads that stores both the number of times an ad is displayed and how many times it gets clicked. These are stored in two different tables (since different information is stored for each) but both have identical name columns. I am trying to display both # times displayed and # times clicked in the same table in an admin page. Here is my query: SELECT ads_clickrate.name, ads_clickrate.link, SUM(ads_displayrate.count) as display, COUNT(ads_clickrate.date) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name WHERE YEAR(ads_clickrate.date) = '2003' AND MONTH(ads_clickrate.date) = '01' GROUP BY ads_clickrate.name ORDER BY ads_clickrate.name I want to display for each ad the number of times displayed, and then number of times clicked if applicable, if not 0. The query is only returning rows for ads that have been clicked on. Is the problem because I have a COUNT column for one table, with a group by? Is this causing the display to be grouped also? If you need more information how the table is set up let me know, I'm really stumped here. Thanks, -Lisi -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] LEFT JOIN not working
Oops! Sorry, I missed it the first time. Your query should start as: SELECT ads_displayrate.name instead of SELECT ads_clickrate.name then you will always have a non-NULL name (coming from the table on the left of the LEFT JOIN). HTH Ignatius, from Brussels Where the fuck is Belgium? D. Ivester, CEO, Coca Cola - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 1:11 PM Subject: Re: [PHP-DB] LEFT JOIN not working Exactly my question - why does it not have a name? How would I modify my query to get a row with a name but null value for date? I thought the join would take care of this, but I'm obviously not doing it right. You mention a unique identifier, there is a separate table with a row for each ad, containing name, URL, and a unique ID number (autoincrement). Should this table be included somehow in the query? How would this help? Thanks, -Lisi At 12:45 PM 1/9/03 +0100, Ignatius Reilly wrote: Your 4th row ought to have an identifier of some sort. From your SELECT statement, this seems to be the name. Why does it not have a name? Probably what you want is a row with a name but a NULL value for ads_clickrate.date. (by the way it is EXTREMELY advisable to use an abstract identifier, such as an id, unique and required, instead of name) Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 12:18 PM Subject: Re: [PHP-DB] LEFT JOIN not working OK, this helped a bit. Now I have, in addition to the three rows of ads that have ben clicked on, a fourth row with no ad name, 0 clickthroughs, and 24 displays. That plus the other three account for all the displayed ads. However, since it is returning a null value for any ad name that has not been clicked on, and then it's grouped by ad name, it lumps all non-clicked ads into one row. What I need is to see each ad on a separate row, which is what I thought a LEFT JOIN was supposed to do. Any suggestions? Thanks, -Lisi At 11:07 AM 1/9/03 +0100, Ignatius Reilly wrote: problem 1: move the WHERE clauses to the ON clauses problem 2: Obviously your intent with COUNT(ads_clickrate.date) is to count the number of non-null occurences of click. But COUNT() is not the appropriate function to do this (it will simply give you the number of rows inside a group). Try replacing COUNT(ads_clickrate.date) by SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 10:44 AM Subject: [PHP-DB] LEFT JOIN not working I have a page with many ads that stores both the number of times an ad is displayed and how many times it gets clicked. These are stored in two different tables (since different information is stored for each) but both have identical name columns. I am trying to display both # times displayed and # times clicked in the same table in an admin page. Here is my query: SELECT ads_clickrate.name, ads_clickrate.link, SUM(ads_displayrate.count) as display, COUNT(ads_clickrate.date) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name WHERE YEAR(ads_clickrate.date) = '2003' AND MONTH(ads_clickrate.date) = '01' GROUP BY ads_clickrate.name ORDER BY ads_clickrate.name I want to display for each ad the number of times displayed, and then number of times clicked if applicable, if not 0. The query is only returning rows for ads that have been clicked on. Is the problem because I have a COUNT column for one table, with a group by? Is this causing the display to be grouped also? If you need more information how the table is set up let me know, I'm really stumped here. Thanks, -Lisi -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] LEFT JOIN not working
Oops! I missed again. If you specify conditions pertaining to the right-hand table, such as: ads_clickrate.date = '2001', Then you will lose all result rows for which the right-hand data is NULL. Not the expected result. So your restricting WHERE clauses must apply to the left-hand table only. Therefore: WHERE YEAR(ads_displayrate.date) = '2003' AND MONTH(ads_displayrate.date) = '01' (if your table ads_displayrate has such date fields). HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 6:54 PM Subject: Re: [PHP-DB] LEFT JOIN not working Cool! It's mostly working now, the only problem is it's ignoring the other clauses in the ON clause that select the desired date. Perhaps it's not supposed to be connected this way? How would I select specific dates? Thanks again, -Lisi At 01:20 PM 1/9/03 +0100, you wrote: Oops! Sorry, I missed it the first time. Your query should start as: SELECT ads_displayrate.name instead of SELECT ads_clickrate.name then you will always have a non-NULL name (coming from the table on the left of the LEFT JOIN). HTH Ignatius, from Brussels Where the fuck is Belgium? D. Ivester, CEO, Coca Cola - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 1:11 PM Subject: Re: [PHP-DB] LEFT JOIN not working Exactly my question - why does it not have a name? How would I modify my query to get a row with a name but null value for date? I thought the join would take care of this, but I'm obviously not doing it right. You mention a unique identifier, there is a separate table with a row for each ad, containing name, URL, and a unique ID number (autoincrement). Should this table be included somehow in the query? How would this help? Thanks, -Lisi At 12:45 PM 1/9/03 +0100, Ignatius Reilly wrote: Your 4th row ought to have an identifier of some sort. From your SELECT statement, this seems to be the name. Why does it not have a name? Probably what you want is a row with a name but a NULL value for ads_clickrate.date. (by the way it is EXTREMELY advisable to use an abstract identifier, such as an id, unique and required, instead of name) Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: Ignatius Reilly [EMAIL PROTECTED]; PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 12:18 PM Subject: Re: [PHP-DB] LEFT JOIN not working OK, this helped a bit. Now I have, in addition to the three rows of ads that have ben clicked on, a fourth row with no ad name, 0 clickthroughs, and 24 displays. That plus the other three account for all the displayed ads. However, since it is returning a null value for any ad name that has not been clicked on, and then it's grouped by ad name, it lumps all non-clicked ads into one row. What I need is to see each ad on a separate row, which is what I thought a LEFT JOIN was supposed to do. Any suggestions? Thanks, -Lisi At 11:07 AM 1/9/03 +0100, Ignatius Reilly wrote: problem 1: move the WHERE clauses to the ON clauses problem 2: Obviously your intent with COUNT(ads_clickrate.date) is to count the number of non-null occurences of click. But COUNT() is not the appropriate function to do this (it will simply give you the number of rows inside a group). Try replacing COUNT(ads_clickrate.date) by SUM( IF( ads_clickrate.date IS NULL, 0, 1 ) ) HTH Ignatius - Original Message - From: Lisi [EMAIL PROTECTED] To: PHP-DB [EMAIL PROTECTED] Sent: Thursday, January 09, 2003 10:44 AM Subject: [PHP-DB] LEFT JOIN not working I have a page with many ads that stores both the number of times an ad is displayed and how many times it gets clicked. These are stored in two different tables (since different information is stored for each) but both have identical name columns. I am trying to display both # times displayed and # times clicked in the same table in an admin page. Here is my query: SELECT ads_clickrate.name, ads_clickrate.link, SUM(ads_displayrate.count) as display, COUNT(ads_clickrate.date) as click FROM ads_displayrate LEFT JOIN ads_clickrate ON ads_displayrate.name = ads_clickrate.name WHERE YEAR(ads_clickrate.date) = '2003' AND MONTH(ads_clickrate.date) = '01' GROUP BY ads_clickrate.name ORDER BY ads_clickrate.name
Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
Hi, Did you try to use something like this. mysql_query(SELECT DISTINCT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM ((WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID) LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID) LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); The other thing that may help is to rearange the order of the tables in the FROM clause. Please check if some of the joins return more than one result - if you have more than one address for a publisher the query will return one row for every address. Hope this helps Dobromir Velev -Original Message- From: Mike Gifford [EMAIL PROTECTED] To: [EMAIL PROTECTED] [EMAIL PROTECTED] Date: Thursday, July 12, 2001 4:31 AM Subject: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Hello, I posted this to the general list this morning got a couple of good leads, but they weren't able to actually fix the problem, so I'm posting here to the db list. I'm making some headway on joining three MySQL tables. However, when I run this query: mysql_query(SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); I now get results in triplicate. ie. I'm getting three copies of the same title, firstName, organization, etc I somehow suspected that this should be the result with LEFT JOIN, but I'm not sure how to return a query without duplication. This is far better than what I had this morning (which was no response from the server). Thanks. I'm new to joining tables... Someone wrote back suggesting that SELECT DISTINCT could be used to to the job. Another person suggested that using UNIQUE(profileID) would make it look nicer. I wasn't sure how to use UNIQUE with the last JOIN as it isn't directly linked to WLPbib.. Any suggestions would be useful. Mike -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ It is a miracle that curiosity survives formal education. - A Einstein -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
What about... SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROMWLPprofile, WLPaddress, WLPcountry WHERE WLPprofile.profileID = WLPbib.profileID AND WLPaddress.publisherID = WLPbib.publisherID AND WLPcountry.countryID = WLPaddress.countryID; I had a similar problems with a database I was working with... It had a main table with 29,000 listings, and I decided to normalize it to improve query times (split the records into diff. tables, one for each attribute of the record, associate the records back together by a common ID than spanned all the tables)... I ended up with an SQL query that spanned like 10 tables-- but it was -way- faster than one big table. There's an excellent article on Normalization on PHPbuilder -- http://www.phpbuilder.com/columns/barry2731.php3 If your WLP tables are very large, you may want to try using mySQL's EXPLAIN SELECT [rest of select query]... function to figure out the best (read: efficient) ways of performing this query... Indexes are definitely a must if you are dealing with a lot of rows... If not, you should be just fine with the above query-- which I -think- is equivilent... I don't have a ton of experience with SQL, so perhaps someone can better elaborate. Best of luck! -Original Message- From: Dobromir Velev [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 12, 2001 3:01 AM To: [EMAIL PROTECTED]; Mike Gifford Subject: Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Hi, Did you try to use something like this. mysql_query(SELECT DISTINCT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM ((WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID) LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID) LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); The other thing that may help is to rearange the order of the tables in the FROM clause. Please check if some of the joins return more than one result - if you have more than one address for a publisher the query will return one row for every address. Hope this helps Dobromir Velev -Original Message- From: Mike Gifford [EMAIL PROTECTED] To: [EMAIL PROTECTED] [EMAIL PROTECTED] Date: Thursday, July 12, 2001 4:31 AM Subject: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Hello, I posted this to the general list this morning got a couple of good leads, but they weren't able to actually fix the problem, so I'm posting here to the db list. I'm making some headway on joining three MySQL tables. However, when I run this query: mysql_query(SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); I now get results in triplicate. ie. I'm getting three copies of the same title, firstName, organization, etc I somehow suspected that this should be the result with LEFT JOIN, but I'm not sure how to return a query without duplication. This is far better than what I had this morning (which was no response from the server). Thanks. I'm new to joining tables... Someone wrote back suggesting that SELECT DISTINCT could be used to to the job. Another person suggested that using UNIQUE(profileID) would make it look nicer. I wasn't sure how to use UNIQUE with the last JOIN as it isn't directly linked to WLPbib.. Any suggestions would be useful. Mike -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ It is a miracle that curiosity survives formal education. - A Einstein -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED
Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
Hi Matthew, Matthew Loff wrote: What about... SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPprofile, WLPaddress, WLPcountry WHERE WLPprofile.profileID = WLPbib.profileID AND WLPaddress.publisherID = WLPbib.publisherID AND WLPcountry.countryID = WLPaddress.countryID; I think that this is working now. I had a similar problems with a database I was working with... It had a main table with 29,000 listings, and I decided to normalize it to improve query times (split the records into diff. tables, one for each attribute of the record, associate the records back together by a common ID than spanned all the tables)... This one will likely have 10,000 to start with, so it is good to build in normalization... Didn't even know what the term normalization referred to earlier today. However, that is what I was doing with the table. I ended up with an SQL query that spanned like 10 tables-- but it was -way- faster than one big table. There's an excellent article on Normalization on PHPbuilder -- http://www.phpbuilder.com/columns/barry2731.php3 This is a good article. Even addressed the question that I fired off to Dobromir about linking various profiles to the same bibliography. One person can write many articles an article can have many authors, so I need to create a many-many table to link the articles to profiles. I think that this wil work: CREATE TABLE WLParticle2profile ( a2pID mediumint(9) NOT NULL auto_increment, bibID mediumint(9), profileID mediumint(9), PRIMARY KEY (a2pID) ) I'll then need to Re-jig the WHERE command to limit the number of returns... This would become SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROMWLPprofile, WLPaddress, WLPcountry, WLParticle2profile WHERE WLParticle2profile.profileID = WLPbib.profileID AND WLPaddress.publisherID = WLPbib.publisherID AND WLPcountry.countryID = WLPaddress.countryID; I'm not sure this will work to tie in the relational table and then we come back to the problem with duplicate entries again... If your WLP tables are very large, you may want to try using mySQL's EXPLAIN SELECT [rest of select query]... function to figure out the best (read: efficient) ways of performing this query... Indexes are definitely a must if you are dealing with a lot of rows... If not, you should be just fine with the above query-- which I -think- is equivilent... I didn't know about this option: http://www.mysql.com/doc/E/X/EXPLAIN.html I find the MySQL.com site to be hard to read through.. php.net is much easier to understand in my experience. I don't have a ton of experience with SQL, so perhaps someone can better elaborate. I think that you did a good job.. However I think I'm still stuck with the same duplicate error now (well when I've expanded the code. Mike -Original Message- From: Dobromir Velev [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 12, 2001 3:01 AM To: [EMAIL PROTECTED]; Mike Gifford Subject: Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Hi, Did you try to use something like this. mysql_query(SELECT DISTINCT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM ((WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID) LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID) LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); The other thing that may help is to rearange the order of the tables in the FROM clause. Please check if some of the joins return more than one result - if you have more than one address for a publisher the query will return one row for every address. Hope this helps Dobromir Velev -Original Message- From: Mike Gifford [EMAIL PROTECTED] To: [EMAIL PROTECTED] [EMAIL PROTECTED] Date: Thursday, July 12, 2001 4:31 AM Subject: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Hello, I posted this to the general list this morning got a couple of good leads, but they weren't able to actually fix the problem, so I'm posting here to the db list. I'm making some headway on joining three MySQL tables
RE: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
Mike-- I hate to suggest quick fixes :) but if all else fails, you can add a DISTINCT to the query. I've had to do it before... If you keep running into problems, and EXPLAIN ... doesn't resolve them, then perhaps you could send us a dump of the db structure... # mysqldump -d [database_name] dumpfile.sql Or if you need a UN/PW: # mysqldump -d [database_name] -u[username] -p dumpfile.sql That will dump the structure of the db, but not the actual data. Good luck! -Original Message- From: Mike Gifford [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 12, 2001 10:48 AM To: Matthew Loff Cc: 'Dobromir Velev'; [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Hi Matthew, Matthew Loff wrote: What about... SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPprofile, WLPaddress, WLPcountry WHERE WLPprofile.profileID = WLPbib.profileID AND WLPaddress.publisherID = WLPbib.publisherID AND WLPcountry.countryID = WLPaddress.countryID; I think that this is working now. I had a similar problems with a database I was working with... It had a main table with 29,000 listings, and I decided to normalize it to improve query times (split the records into diff. tables, one for each attribute of the record, associate the records back together by a common ID than spanned all the tables)... This one will likely have 10,000 to start with, so it is good to build in normalization... Didn't even know what the term normalization referred to earlier today. However, that is what I was doing with the table. I ended up with an SQL query that spanned like 10 tables-- but it was -way- faster than one big table. There's an excellent article on Normalization on PHPbuilder -- http://www.phpbuilder.com/columns/barry2731.php3 This is a good article. Even addressed the question that I fired off to Dobromir about linking various profiles to the same bibliography. One person can write many articles an article can have many authors, so I need to create a many-many table to link the articles to profiles. I think that this wil work: CREATE TABLE WLParticle2profile ( a2pID mediumint(9) NOT NULL auto_increment, bibID mediumint(9), profileID mediumint(9), PRIMARY KEY (a2pID) ) I'll then need to Re-jig the WHERE command to limit the number of returns... This would become SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROMWLPprofile, WLPaddress, WLPcountry, WLParticle2profile WHERE WLParticle2profile.profileID = WLPbib.profileID AND WLPaddress.publisherID = WLPbib.publisherID AND WLPcountry.countryID = WLPaddress.countryID; I'm not sure this will work to tie in the relational table and then we come back to the problem with duplicate entries again... If your WLP tables are very large, you may want to try using mySQL's EXPLAIN SELECT [rest of select query]... function to figure out the best (read: efficient) ways of performing this query... Indexes are definitely a must if you are dealing with a lot of rows... If not, you should be just fine with the above query-- which I -think- is equivilent... I didn't know about this option: http://www.mysql.com/doc/E/X/EXPLAIN.html I find the MySQL.com site to be hard to read through.. php.net is much easier to understand in my experience. I don't have a ton of experience with SQL, so perhaps someone can better elaborate. I think that you did a good job.. However I think I'm still stuck with the same duplicate error now (well when I've expanded the code. Mike -Original Message- From: Dobromir Velev [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 12, 2001 3:01 AM To: [EMAIL PROTECTED]; Mike Gifford Subject: Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Hi, Did you try to use something like this. mysql_query(SELECT DISTINCT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM ((WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID) LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID) LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID
Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
Mike - I'm not certain but it sounds like you might be looking for GROUP BY. Do GROUP BY and then the columns that are identical in your results. - Ken At 09:46 PM 7/11/01 -0400, Mike Gifford wrote: Hello, I posted this to the general list this morning got a couple of good leads, but they weren't able to actually fix the problem, so I'm posting here to the db list. I'm making some headway on joining three MySQL tables. However, when I run this query: mysql_query(SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); I now get results in triplicate. ie. I'm getting three copies of the same title, firstName, organization, etc I somehow suspected that this should be the result with LEFT JOIN, but I'm not sure how to return a query without duplication. This is far better than what I had this morning (which was no response from the server). Thanks. I'm new to joining tables... Someone wrote back suggesting that SELECT DISTINCT could be used to to the job. Another person suggested that using UNIQUE(profileID) would make it look nicer. I wasn't sure how to use UNIQUE with the last JOIN as it isn't directly linked to WLPbib.. Any suggestions would be useful. Mike -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
Mike - In general, you should always be reading the manual first. From the SELECT syntax at http://www.mysql.com/doc/S/E/SELECT.html GROUP BY must be indicated before ORDER BY. So reverse the order of those portions. - Ken At 10:35 PM 7/11/01 -0400, Mike Gifford wrote: It looked good, but it gave me an error.. Sorry ... WLPcountry.countryID ORDER BY WLPbib.title GROUP BY WLPbib.bibID MySQL Error: 1064 (You have an error in your SQL syntax near 'GROUP BY WLPbib.bibID ' at line 10) Please contact the webmaster and report the exact error message. Session halted. Ken wrote: Mike - I'm not certain but it sounds like you might be looking for GROUP BY. Do GROUP BY and then the columns that are identical in your results. - Ken At 09:46 PM 7/11/01 -0400, Mike Gifford wrote: Hello, I posted this to the general list this morning got a couple of good leads, but they weren't able to actually fix the problem, so I'm posting here to the db list. I'm making some headway on joining three MySQL tables. However, when I run this query: mysql_query(SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); I now get results in triplicate. ie. I'm getting three copies of the same title, firstName, organization, etc I somehow suspected that this should be the result with LEFT JOIN, but I'm not sure how to return a query without duplication. This is far better than what I had this morning (which was no response from the server). Thanks. I'm new to joining tables... Someone wrote back suggesting that SELECT DISTINCT could be used to to the job. Another person suggested that using UNIQUE(profileID) would make it look nicer. I wasn't sure how to use UNIQUE with the last JOIN as it isn't directly linked to WLPbib.. Any suggestions would be useful. Mike -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ It is a miracle that curiosity survives formal education. - A Einstein -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
Sorry Ken, I'm trying to learn a few too many things at once. Switching it around eliminated the error message but not the duplication. I'm eliminating as much of the code as I can to see that it isn't coming from PHP. Thanks again for your help. Mike Ken wrote: In general, you should always be reading the manual first. From the SELECT syntax at http://www.mysql.com/doc/S/E/SELECT.html GROUP BY must be indicated before ORDER BY. So reverse the order of those portions. - Ken At 10:35 PM 7/11/01 -0400, Mike Gifford wrote: It looked good, but it gave me an error.. Sorry ... WLPcountry.countryID ORDER BY WLPbib.title GROUP BY WLPbib.bibID MySQL Error: 1064 (You have an error in your SQL syntax near 'GROUP BY WLPbib.bibID ' at line 10) Please contact the webmaster and report the exact error message. Session halted. Ken wrote: Mike - I'm not certain but it sounds like you might be looking for GROUP BY. Do GROUP BY and then the columns that are identical in your results. - Ken At 09:46 PM 7/11/01 -0400, Mike Gifford wrote: Hello, I posted this to the general list this morning got a couple of good leads, but they weren't able to actually fix the problem, so I'm posting here to the db list. I'm making some headway on joining three MySQL tables. However, when I run this query: mysql_query(SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); I now get results in triplicate. ie. I'm getting three copies of the same title, firstName, organization, etc I somehow suspected that this should be the result with LEFT JOIN, but I'm not sure how to return a query without duplication. This is far better than what I had this morning (which was no response from the server). Thanks. I'm new to joining tables... Someone wrote back suggesting that SELECT DISTINCT could be used to to the job. Another person suggested that using UNIQUE(profileID) would make it look nicer. I wasn't sure how to use UNIQUE with the last JOIN as it isn't directly linked to WLPbib.. Any suggestions would be useful. Mike -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ It is a miracle that curiosity survives formal education. - A Einstein -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ It is a miracle that curiosity survives formal education. - A Einstein -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
Just build the SQL statement piece by piece instead of all at once. Be sure that your tables are setup properly, allowing you to make joins properly. Try not to rush, cause you might miss a couple of little things along the way. Kevin Johnson - Original Message - From: Mike Gifford [EMAIL PROTECTED] To: Ken [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Wednesday, July 11, 2001 08:00 PM Subject: Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables Sorry Ken, I'm trying to learn a few too many things at once. Switching it around eliminated the error message but not the duplication. I'm eliminating as much of the code as I can to see that it isn't coming from PHP. Thanks again for your help. Mike Ken wrote: In general, you should always be reading the manual first. From the SELECT syntax at http://www.mysql.com/doc/S/E/SELECT.html GROUP BY must be indicated before ORDER BY. So reverse the order of those portions. - Ken At 10:35 PM 7/11/01 -0400, Mike Gifford wrote: It looked good, but it gave me an error.. Sorry ... WLPcountry.countryID ORDER BY WLPbib.title GROUP BY WLPbib.bibID MySQL Error: 1064 (You have an error in your SQL syntax near 'GROUP BY WLPbib.bibID ' at line 10) Please contact the webmaster and report the exact error message. Session halted. Ken wrote: Mike - I'm not certain but it sounds like you might be looking for GROUP BY. Do GROUP BY and then the columns that are identical in your results. - Ken At 09:46 PM 7/11/01 -0400, Mike Gifford wrote: Hello, I posted this to the general list this morning got a couple of good leads, but they weren't able to actually fix the problem, so I'm posting here to the db list. I'm making some headway on joining three MySQL tables. However, when I run this query: mysql_query(SELECT WLPbib.bibID, WLPbib.title, WLPbib.publisher, WLPbib.publicationDate, WLPaddress.city, WLPaddress.state, WLPprofile.firstName, WLPprofile.lastName, WLPprofile.organization, WLPcountry.languageName FROM WLPbib LEFT JOIN WLPprofile ON WLPprofile.profileID = WLPbib.profileID LEFT JOIN WLPaddress ON WLPaddress.publisherID = WLPbib.publisherID LEFT JOIN WLPcountry ON WLPcountry.countryID = WLPaddress.countryID); I now get results in triplicate. ie. I'm getting three copies of the same title, firstName, organization, etc I somehow suspected that this should be the result with LEFT JOIN, but I'm not sure how to return a query without duplication. This is far better than what I had this morning (which was no response from the server). Thanks. I'm new to joining tables... Someone wrote back suggesting that SELECT DISTINCT could be used to to the job. Another person suggested that using UNIQUE(profileID) would make it look nicer. I wasn't sure how to use UNIQUE with the last JOIN as it isn't directly linked to WLPbib.. Any suggestions would be useful. Mike -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ It is a miracle that curiosity survives formal education. - A Einstein -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ It is a miracle that curiosity survives formal education. - A Einstein -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
altho this may not help at all, rather than bashing away with php+SQL statements, i usually just pilot my sql in phpmyadmin, then once it works with set values (ie SELECT * FROM people WHERE personID='3' rahter than personID='$personID' or something) i can then drop that SQL into my script. HTH Beau // -Original Message- // From: Mike Gifford [mailto:[EMAIL PROTECTED]] // Sent: Thursday, 12 July 2001 11:01 AM // To: Ken // Cc: [EMAIL PROTECTED] // Subject: Re: [PHP-DB] Left Join is producing duplicate // results - MySQL // relational tables // // // Sorry Ken, // // I'm trying to learn a few too many things at once. // Switching it around // eliminated the error message but not the duplication. // // I'm eliminating as much of the code as I can to see that it // isn't coming from PHP. // // Thanks again for your help. // // Mike // // Ken wrote: // // In general, you should always be reading the manual first. // From the SELECT syntax at // http://www.mysql.com/doc/S/E/SELECT.html // GROUP BY must be indicated before ORDER BY. So reverse // the order of those portions. // // - Ken // // At 10:35 PM 7/11/01 -0400, Mike Gifford wrote: // // It looked good, but it gave me an error.. Sorry // ... // WLPcountry.countryID ORDER BY WLPbib.title GROUP BY WLPbib.bibID // // MySQL Error: 1064 (You have an error in your SQL syntax // near 'GROUP BY WLPbib.bibID ' at line 10) // Please contact the webmaster and report the exact error message. // Session halted. // // Ken wrote: // // // Mike - // I'm not certain but it sounds like you might be looking // for GROUP BY. Do GROUP BY and then the columns that are // identical in your results. // - Ken // At 09:46 PM 7/11/01 -0400, Mike Gifford wrote: // // // Hello, // // I posted this to the general list this morning got a // couple of good leads, but they weren't able to actually fix // the problem, so I'm posting here to the db list. // // I'm making some headway on joining three MySQL tables. // // However, when I run this query: // // mysql_query(SELECT // WLPbib.bibID, // WLPbib.title, // WLPbib.publisher, // WLPbib.publicationDate, // WLPaddress.city, // WLPaddress.state, // WLPprofile.firstName, // WLPprofile.lastName, // WLPprofile.organization, // WLPcountry.languageName // // FROM WLPbib // // LEFT JOIN WLPprofile ON WLPprofile.profileID = // WLPbib.profileID // LEFT JOIN WLPaddress ON WLPaddress.publisherID = // WLPbib.publisherID // LEFT JOIN WLPcountry ON WLPcountry.countryID = // WLPaddress.countryID); // // I now get results in triplicate. ie. I'm getting three // copies of the same title, firstName, organization, etc // // I somehow suspected that this should be the result with // LEFT JOIN, but I'm not sure how to return a query without // duplication. // // This is far better than what I had this morning (which // was no response from the server). // // Thanks. I'm new to joining tables... // // Someone wrote back suggesting that SELECT DISTINCT could // be used to to the job. // // Another person suggested that using UNIQUE(profileID) // would make it look nicer. I wasn't sure how to use UNIQUE // with the last JOIN as it isn't directly linked to WLPbib.. // // Any suggestions would be useful. // // Mike // // // // -- // Mike Gifford, OpenConcept Consulting, http://openconcept.ca // Offering everything your organization needs for an // effective web site. // Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ // It is a miracle that curiosity survives formal education. // - A Einstein // // // -- // PHP Database Mailing List (http://www.php.net/) // To unsubscribe, e-mail: [EMAIL PROTECTED] // For additional commands, e-mail: [EMAIL PROTECTED] // To contact the list administrators, e-mail: // [EMAIL PROTECTED] // // // // // // // -- // Mike Gifford, OpenConcept Consulting, http://openconcept.ca // Offering everything your organization needs for an effective // web site. // Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ // It is a miracle that curiosity survives formal education. - // A Einstein // // // -- // PHP Database Mailing List (http://www.php.net/) // To unsubscribe, e-mail: [EMAIL PROTECTED] // For additional commands, e-mail: [EMAIL PROTECTED] // To contact the list administrators, e-mail: // [EMAIL PROTECTED] // -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Left Join is producing duplicate results - MySQL relational tables
phpMyAdmin is great! And I hear that they are coming out with a new version pretty soon (or is it here - haven't checked).. However I don't think that you can join tables in phpMyAdmin.. Perhaps I don't know the app well enough though. Mike Beau Lebens wrote: altho this may not help at all, rather than bashing away with php+SQL statements, i usually just pilot my sql in phpmyadmin, then once it works with set values (ie SELECT * FROM people WHERE personID='3' rahter than personID='$personID' or something) i can then drop that SQL into my script. HTH Beau // -Original Message- // From: Mike Gifford [mailto:[EMAIL PROTECTED]] // Sent: Thursday, 12 July 2001 11:01 AM // To: Ken // Cc: [EMAIL PROTECTED] // Subject: Re: [PHP-DB] Left Join is producing duplicate // results - MySQL // relational tables // // // Sorry Ken, // // I'm trying to learn a few too many things at once. // Switching it around // eliminated the error message but not the duplication. // // I'm eliminating as much of the code as I can to see that it // isn't coming from PHP. // // Thanks again for your help. // // Mike // // Ken wrote: // // In general, you should always be reading the manual first. // From the SELECT syntax at // http://www.mysql.com/doc/S/E/SELECT.html // GROUP BY must be indicated before ORDER BY. So reverse // the order of those portions. // // - Ken // // At 10:35 PM 7/11/01 -0400, Mike Gifford wrote: // // It looked good, but it gave me an error.. Sorry // ... // WLPcountry.countryID ORDER BY WLPbib.title GROUP BY WLPbib.bibID // // MySQL Error: 1064 (You have an error in your SQL syntax // near 'GROUP BY WLPbib.bibID ' at line 10) // Please contact the webmaster and report the exact error message. // Session halted. // // Ken wrote: // // // Mike - // I'm not certain but it sounds like you might be looking // for GROUP BY. Do GROUP BY and then the columns that are // identical in your results. // - Ken // At 09:46 PM 7/11/01 -0400, Mike Gifford wrote: // // // Hello, // // I posted this to the general list this morning got a // couple of good leads, but they weren't able to actually fix // the problem, so I'm posting here to the db list. // // I'm making some headway on joining three MySQL tables. // // However, when I run this query: // // mysql_query(SELECT // WLPbib.bibID, // WLPbib.title, // WLPbib.publisher, // WLPbib.publicationDate, // WLPaddress.city, // WLPaddress.state, // WLPprofile.firstName, // WLPprofile.lastName, // WLPprofile.organization, // WLPcountry.languageName // // FROM WLPbib // // LEFT JOIN WLPprofile ON WLPprofile.profileID = // WLPbib.profileID // LEFT JOIN WLPaddress ON WLPaddress.publisherID = // WLPbib.publisherID // LEFT JOIN WLPcountry ON WLPcountry.countryID = // WLPaddress.countryID); // // I now get results in triplicate. ie. I'm getting three // copies of the same title, firstName, organization, etc // // I somehow suspected that this should be the result with // LEFT JOIN, but I'm not sure how to return a query without // duplication. // // This is far better than what I had this morning (which // was no response from the server). // // Thanks. I'm new to joining tables... // // Someone wrote back suggesting that SELECT DISTINCT could // be used to to the job. // // Another person suggested that using UNIQUE(profileID) // would make it look nicer. I wasn't sure how to use UNIQUE // with the last JOIN as it isn't directly linked to WLPbib.. // // Any suggestions would be useful. // // Mike // // // // -- // Mike Gifford, OpenConcept Consulting, http://openconcept.ca // Offering everything your organization needs for an // effective web site. // Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ // It is a miracle that curiosity survives formal education. // - A Einstein // // // -- // PHP Database Mailing List (http://www.php.net/) // To unsubscribe, e-mail: [EMAIL PROTECTED] // For additional commands, e-mail: [EMAIL PROTECTED] // To contact the list administrators, e-mail: // [EMAIL PROTECTED] // // // // // // // -- // Mike Gifford, OpenConcept Consulting, http://openconcept.ca // Offering everything your organization needs for an effective // web site. // Abolish Nuclear Weapons Now!: http://pgs.ca/petition/ // It is a miracle that curiosity survives formal education. - // A Einstein // // // -- // PHP Database Mailing List (http://www.php.net/) // To unsubscribe, e-mail: [EMAIL PROTECTED] // For additional commands, e-mail: [EMAIL PROTECTED] // To contact the list administrators, e-mail: // [EMAIL PROTECTED] // -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything